使用变量从python执行shell脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18742657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:40:18  来源:igfitidea点击:

Execute shell script from python with a variable

pythonvariablesexecute

提问by Vince

I have this code:

我有这个代码:

opts.info("Started domain %s (id=%d)" % (dom, domid))

I want to execute a shell script with the parameter domidfrom above. Something like this:

我想用domid上面的参数执行一个shell脚本。像这样的东西:

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

采纳答案by Paco

Like this ?

像这样 ?

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

Documentation is available on the python website

文档可在python 网站上找到

回答by Prahalad Deshpande

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

您需要从您的 python 脚本以下列方式调用 shell 脚本:

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

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

有关 subprocess 模块的文档,请参阅此处。在上面的脚本中,我们将一个列表传递给call方法,其中第一个元素是要执行的程序,列表中的其余元素是程序的参数。

回答by LearnOPhile

A simple solution to remember:

要记住的简单解决方案:

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

回答by Murtaza Pitalwala

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).

我也想做和这篇文章一样的事情。使用变量从python执行Shell脚本(使用变量我认为这意味着使用命令行参数)。

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.

我也,经过更多阅读后,它建议最好使用 subprocess.Popen,如果您想将结果返回用于显示目的。我正在使用 bash 脚本将所有内容都记录到另一个文件中,所以我真的不需要子进程。

I hope it helps.

我希望它有帮助。

    import os
    os.system("cat /root/test.sh")
    #!/bin/bash
    x='1'
    while [[ $x -le 10 ]] ; do
      echo $x: hello   
      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