how to set/get shell env variable in python script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to set/get shell env variable in python script
# 1  
Old 11-28-2011
how to set/get shell env variable in python script

greetings,

i have a sh script that calls a python script. the sh script sets an env variable BIN:
Code:
export BIN=bin64

i need to get that BIN variable's value and use it within this python script. anyone know how to do this? thanx in advance.
# 2  
Old 11-28-2011
# 3  
Old 11-28-2011
already read that info and this is what i tried and failed

mpiRoot is passed as a command line option:
Code:
MPI_ROOTDIR=mpiRoot
os.getenv(BIN)
mpdCmd="%s/BIN/mpd" % MPI_ROOTDIR
mpdtraceCmd="%s/BIN/mpdtrace" % MPI_ROOTDIR
mpdtraceCmd2="%s/BIN/mpdtrace -l" % MPI_ROOTDIR

although i replaced the os.getenv with below and succeeded but looking for a more efficient way.
Code:
MPI_ROOTDIR=mpiRoot
if MPI_ROOTDIR == "/usr/apps/intel/impi/3.2.2.006":
    Bin="bin64"
else:
    Bin="bin"
mpdCmd="%s/%s/mpd" % (MPI_ROOTDIR, Bin)
mpdtraceCmd="%s/%s/mpdtrace" % (MPI_ROOTDIR, Bin)
mpdtraceCmd2="%s/%s/mpdtrace -l" % (MPI_ROOTDIR, Bin)

# 4  
Old 11-28-2011
It works for me! Check below:
Code:
# export BIN="123456789"
# python
Python 2.3.4 (#1, Jul 16 2009, 07:03:37)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.getenv('BIN')
123456789

If you are using: "os.getenv(BIN)" and BIN, in this context in not a variable, it will not work! It must be between quotes (single or double).

I hope it helps.
# 5  
Old 11-28-2011
that works for me as well HOWEVER:

Code:
# export BIN=bin64
# python
Python 2.4.3 (#1, Jun 11 2009, 14:09:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.getenv('BIN')
bin64
>>>

how do i get that "BIN" to be recognized on the code i posted in my second post? therein lies my issue. thanx.
# 6  
Old 11-28-2011
In the same way as you are doing as a command line argument:
Code:
def getEnvVarValue(envVar):
    tmpVal = os.getenv(envVar)
    if not tmpVal:
        print("Environment variable: [%s] not set." % (envVar))
        sys.exit(1)
        
    retValue = tmpVal.strip("'") 
    return retValue


Bin=getEnvVarValue('BIN')
mpdCmd="%s/%s/mpd" % (MPI_ROOTDIR, Bin)
mpdtraceCmd="%s/%s/mpdtrace" % (MPI_ROOTDIR, Bin)
mpdtraceCmd2="%s/%s/mpdtrace -l" % (MPI_ROOTDIR, Bin)

I hope it helps!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - ENV Variable context problem using su

Hello I have found some piece of code to verify and then run shell script with root permission from normal user. see : http://blog.mecworks.com/articles/2006/02/23/bash-scripting-tip-running-a-script-as-root I have wrote two scripts using this tips. - one to copy file from server to local... (6 Replies)
Discussion started by: jcdole
6 Replies

2. Web Development

Deny from env=env-variable Does not work

(Above from Apache docs). On my system, using: SetEnvIf User-Agent Mozilla IsBad=1 Order allow,deny Allow from all Deny from env=IsBad ...I see that environment variable is set (using phpinfo()) but the page is still served. No errors in the Apache logs. (1 Reply)
Discussion started by: gnurob
1 Replies

3. Shell Programming and Scripting

Passing variable from shell script to python script

I have a shell script main.sh which inturn call the python script ofdm.py, I want to pass two variables from shell script to python script for its execution. How do i achieve this ????? Eg: main.sh a=3 b=3; c= a+b exec python ofdm.py ofdm.py d=c+a Thanks in Anticipation (4 Replies)
Discussion started by: shashi792
4 Replies

4. Shell Programming and Scripting

Set/Export Env Vars from with Shell Script With Input Variable

I have a shell script I want to run that will set environment variables based on the value of an input variable submitted when the shell script is called. For example: $ mgenv.sh prod This would set environment variables for prod $ mgenv.sh test This would set environment variables... (1 Reply)
Discussion started by: brtaylor73
1 Replies

5. UNIX for Advanced & Expert Users

How to export ENV variables, which remains set for all the shell

Hi ! How to export ENV variables, which remains set for all the shell Example :- Login :myID Pwd : **** -> Here my ID .profile is executed. Let say I set MYENV variable Kisses% rlogin ABC -l XXXGroupID -> I login into a remote Solaris Server ABC password : **** -> "XXXGroupID's... (1 Reply)
Discussion started by: dashok.83
1 Replies

6. Shell Programming and Scripting

get env variable from last script

I have 2 scripts t2.sh calls t1.sh. I need to get the vaule of a env variable from t1.sh /tmp/test$ cat t1.sh #!/bin/sh INSTANCE="font/fc-cache" export INSTANCE svcadm disable ${INSTANCE} /tmp/test$ cat t2.sh #!/bin/sh . /tmp/test/t1.sh echo ${INSTANCE} The above works... (9 Replies)
Discussion started by: honglus
9 Replies

7. Shell Programming and Scripting

problem in getting the path of environment variable set in bashrc in my shell script

hi all i have joined new to the group. i have set an variable in my bashrc file. .bashrc PROGHOME=/home/braf/braf/prog export PROGHOME but while using it in my shell script its path is not taken and i had to explicitly give the export command to set the path. in my script... (8 Replies)
Discussion started by: krithika
8 Replies

8. AIX

Do I need to remote after installation of s/w and set the env variable

Hi, I have installed ODWEK software on AIX box and set the environment variables like: PATH,LIBPATH,LD_LIBRARY_PATH,CLASSPATH. My question here is do I need to reboot the system to take these changes or is there anyother workaround. I heard that there is 'export'. But I don't know how far it... (1 Reply)
Discussion started by: srangu
1 Replies

9. AIX

Do I need to reboot after installation of s/w and set the env variable

Hi, I have installed ODWEK software on AIX box and set the environment variables like: PATH,LIBPATH,LD_LIBRARY_PATH,CLASSPATH. My question here is do I need to reboot the system to take these changes or is there anyother workaround. I heard that there is 'export'. But I don't know how far it... (1 Reply)
Discussion started by: srangu
1 Replies

10. Shell Programming and Scripting

Is there any way to set env variable in top level Makefile and unset when done

Hello I have compilation directory structure the top level Makefile is the one that contains all the sub directories I want to set in this Makefile env variable say : setenv OPTIMIZATION_LEVEL "1" and when all the sub directories done compiling it will set this variable to different lavel... (0 Replies)
Discussion started by: umen
0 Replies
Login or Register to Ask a Question