how to pass an integer from python script into bash script?

Question:

I have a python script demo.py and a bash script run_offline.sh. demo.py follows:

import subprocess

path1 = '/xx/xxx/xxxx/xxxx'
argument_int_1 = 5 # or whatever number else

And I want to call and run the run_offline.sh in demo.py, like:

import subprocess

path1 = '/xx/xxx/xxxx/xxxx'
argument_int_1 = 5 # or whatever number else

p = subprocess.Popen(['./run_offline.sh', str(argument_int_1), path1])
p.wait()

And in my bash script, the argument should be organised as following:

path1=${1:-1}
END_ID=${2:-1}

CID=0
while [ $CID -le $END_ID ]; do 
xxxx
done

But I continued getting the error information after I submitted the python script to slurm, like:

Traceback (most recent call last):
  File "demo.py", line 357, in <module>
    p1 = subprocess.Popen(['./run_offline.sh', str(argument_int_1), path1])
  File "/home/eccei339/.conda/envs/vegetation/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/home/eccei339/.conda/envs/vegetation/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: './run_offline.sh'

So does it mean the bash script does not recognise the END_ID as integer? if so, what can I do? Thanks!

Asked By: Xu Shan

||

Answers:

I think you forgot the shebang in your script, the shebang "#!" are the magic bytes used to tell the os that the file is in fact a script and that it should use the interpreter specified after. In your case add this at the top of your shell script:

#!/bin/bash
Answered By: jgiron42