简体   繁体   中英

Execute shell script from python with a variable

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

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

A simple solution to remember:

import os
bashCommand = "source script.sh"
os.system(bashCommand)

You need to call the shell script in the below manner from your python script:

subprocess.call(['test.sh', domid])

Refer here for the documentation of the subprocess module. In the above script, we pass a list to the call method where the first element is the program to be executed and the remaining elements within the list are the arguments to the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM