Execute shell script from python with a variable
I was also looking to do the same thing as this post. Execute Shell Script from python with variable (with variable I think it means with command line argument).
I did the following to get the results. I am sharing in case other people are looking for the same answer.
import os arglist = 'arg1 arg2 arg3' bashCommand = "/bin/bash script.sh " + arglist os.system(bashCommand)
which worked just fine for me.
I also, after more reading it suggests that it would be better to use subprocess.Popen, if you want to get the results back for display purposes. I am logging everything out to another file with in the bash script, so I don't really have a need for subprocess.
I hope it helps.
import os os.system("cat /root/test.sh") #!/bin/bash x='1' while [[ $x -le 10 ]] ; do echo $x: hello $1 $2 $3 sleep 1 x=$(( $x + 1 )) done arglist = 'arg1 arg2 arg3' bashCommand = 'bash /root/test.sh ' + arglist os.system(bashCommand) 1: hello arg1 arg2 arg3 2: hello arg1 arg2 arg3 3: hello arg1 arg2 arg3 4: hello arg1 arg2 arg3 5: hello arg1 arg2 arg3
Like this ?
subprocess.call(['test.sh', str(domid)])
Documentation is available on the python website