16

Eventually I understand this and it works.

bash script:

#!/bin/bash
#$ -V
#$ -cwd
#$ -o $HOME/sge_jobs_output/$JOB_ID.out -j y
#$ -S /bin/bash
#$ -l mem_free=4G


c=$SGE_TASK_ID
cd /home/xxx/scratch/test/
FILENAME=`head -$c testlist|tail -1`
python testpython.py $FILENAME

python script:

#!/bin/python
import sys,os


path='/home/xxx/scratch/test/'
name1=sys.argv[1]
job_id=os.path.join(path+name1)
f=open(job_id,'r').readlines()
print f[1]

thx

LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
  • What part of the `argparse` module confuses you? http://docs.python.org/dev/library/argparse.html. It helps to ask more **specific** questions on problem you're having with your code. Please post code, using `argparse` and ask specific questions. – S.Lott Sep 22 '11 at 20:30
  • Pass it as a command line parameter and then pull it out of sys.argv. – GreenMatt Sep 22 '11 at 20:30

4 Answers4

24

Exported bash variables are actually environment variables. You get at them through the os.environ object with a dictionary-like interface. Note that there are two types of variables in Bash: those local to the current process, and those that are inherited by child processes. Your Python script is a child process, so you need to make sure that you export the variable you want the child process to access.

To answer your original question, you need to first export the variable and then access it from within the python script using os.environ.

##!/bin/bash
#$ -V
#$ -cwd
#$ -o $HOME/sge_jobs_output/$JOB_ID.out -j y
#$ -S /bin/bash
#$ -l mem_free=4G

c=$SGE_TASK_ID
cd /home/xxx/scratch/test/
export FILENAME=`head -$c testlist|tail -1`
chmod +X testpython.py
./testpython.py


#!/bin/python
import sys
import os

for arg in sys.argv:  
    print arg  

f=open('/home/xxx/scratch/test/' + os.environ['FILENAME'],'r').readlines()
print f[1]

Alternatively, you may pass the variable as a command line argument, which is what your code is doing now. In that case, you must look in sys.argv, which is the list of arguments passed to your script. They appear in sys.argv in the same order you specified them when invoking the script. sys.argv[0] always contains the name of the program that's running. Subsequent entries contain other arguments. len(sys.argv) indicates the number of arguments the script received.

#!/bin/python
import sys
import os

if len(sys.argv) < 2:
    print 'Usage: ' + sys.argv[0] + ' <filename>'
    sys.exit(1)

print 'This is the name of the python script: ' + sys.argv[0]
print 'This is the 1st argument:              ' + sys.argv[1]

f=open('/home/xxx/scratch/test/' + sys.argv[1],'r').readlines()
print f[1]
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Nathan
  • 4,777
  • 1
  • 28
  • 35
  • Had to edit the first line to make this accurate. *Unexported* shell variables are certainly not environment variables. – Charles Duffy Jun 17 '19 at 01:36
1

Take a look at parsing Python arguments. Your bash code would be fine, just need to edit your Python script to take the argument.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
aus
  • 1,394
  • 1
  • 14
  • 19
1

use this inside your script (EDITED per Aarons suggestion):

def main(args):
    do_something(args[0])


if __name__ == "__main__":
    import sys
    main(sys.argv[1:])
MattoTodd
  • 14,467
  • 16
  • 59
  • 76
  • 1
    `bool(sys.argv)` is always `True` and `sys.argv[0]` will contain the script name. – wRAR Sep 22 '11 at 20:33
  • 1
    If you're going to pass the arguments to a function, its more common to cut off the script name so the function doesn't need to know to ignore it. i.e. `main(sys.argv[1:])` – Aaron Dufour Sep 22 '11 at 21:13
  • anything you want it to be, i'm just showing you how you can grab the arguments like your question asks – MattoTodd Sep 22 '11 at 21:44
  • if you ask a question, and then get an answer that answers THAT question, then the thread is done. you should not then go, OK now i got a different error, help me fix that. That's not what stackoverflow was intended for – MattoTodd Sep 22 '11 at 21:46
  • Does it handles if I a pass a list as an argument? @MattoTodd – alper Mar 16 '20 at 16:59
0

Command line arguments to the script are available as sys.argv list.

wRAR
  • 25,009
  • 4
  • 84
  • 97