11

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
codeforester
  • 39,467
  • 16
  • 112
  • 140
Vince
  • 1,133
  • 3
  • 17
  • 34

4 Answers4

16

Like this ?

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

Documentation is available on the python website

Paco
  • 4,520
  • 3
  • 29
  • 53
4

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
codeforester
  • 39,467
  • 16
  • 112
  • 140
Murtaza Pitalwala
  • 839
  • 1
  • 8
  • 14
  • Thanks. In the future, I will use more python with linux bash, so I can try your way next time ;-) – Vince Jan 08 '14 at 07:18
1

A simple solution to remember:

import os
bashCommand = "source script.sh"
os.system(bashCommand)
LearnOPhile
  • 1,391
  • 13
  • 16
0

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.

Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22