I have this code:
opts.info("Started domain %s (id=%d)" % (dom, domid))
I want to execute a shell script with the parameter domid
from above.
Something like this:
subprocess.call(['test.sh %d', domid])
How does it work?
I've tried it with:
subprocess.call(['test.sh', domid])
But I get this error:
File "/usr/lib/xen-4.1/bin/xm", line 8, in <module>
main.main(sys.argv)
File "/usr/lib/xen-4.1/bin/../lib/python/xen/xm/main.py", line 3983, in main
_, rc = _run_cmd(cmd, cmd_name, args)
File "/usr/lib/xen-4.1/bin/../lib/python/xen/xm/main.py", line 4007, in _run_cmd
return True, cmd(args)
File "<string>", line 1, in <lambda>
File "/usr/lib/xen-4.1/bin/../lib/python/xen/xm/main.py", line 1519, in xm_importcommand
cmd.main([command] + args)
File "/usr/lib/xen-4.1/bin/../lib/python/xen/xm/create.py", line 1562, in main
dom = make_domain(opts, config)
File "/usr/lib/xen-4.1/bin/../lib/python/xen/xm/create.py", line 1458, in make_domain
subprocess.call(['test.sh', domid])
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
TypeError: execv() arg 2 must contain only strings
If you need to execute a shell command with Python, there are two ways. You can either use the subprocess module or the RunShellCommand() function. The first option is easier to run one line of code and then exit, but it isn't as flexible when using arguments or producing text output.
Fetching Shell Variablesenviron by the desired shell variable's name string (e.g., os. environ['USER'] ) is the moral equivalent of adding a dollar sign before a variable name in most Unix shells (e.g., $USER ), using surrounding percent signs on DOS ( %USER% ), and calling getenv("USER") in a C program.
Instead of entering the input after the script name in bash, you are prompted to input the value after you run the script. Running the script will prompt the user to "enter characters:". After the user enters the characters and then presses ENTER , the permutations will print in the terminal.
Like this ?
subprocess.call(['test.sh', str(domid)])
Documentation is available on the python website
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With