user3413412
user3413412

Reputation: 1

how to access a variable in a python file passed from shell script ..?

how to acsess a variable in a python file passed from shell script ..?

i am exporting a variable xyz from a shell script. Now i want to check it's value in python, file how to do that ?

I have tried using:

#Python
if '$xyz'
flag+= '-DVERSIONNUMBER'

Basically I have to create a macro if xyz holds some value and isn't empty. Can you suggest a way to do that? My another doubt is that can we define a macro selectively in the python file i.e. if xyz is empty I don't want that flag to be defined, so that in the .c files I can check :

#if defined (VERSIONNUMBER)
//code
#else
//code    
#endif

In short following steps I have to execute :

Upvotes: 0

Views: 524

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295403

Environment variables can be accessed via os.environ['varname'].

In your shell:

export VERSIONNUMBER=1

In your Python:

if os.environ.get('VERSIONNUMBER'):
   pass # do something here

Note that the environment is passed in one direction only -- from parents to children. Thus, changing the value for os.environ['VERSIONNUMBER'] from within the Python script will have an effect on any subprocesses it starts (its children), but not the program that started it (its parent).

Upvotes: 3

Related Questions