Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将变量传入命令_Python_Freeswitch - Fatal编程技术网

在python中将变量传入命令

在python中将变量传入命令,python,freeswitch,Python,Freeswitch,我正在编写一个python脚本来执行shell命令,我正在获取参数,并希望将该参数的值传递给命令 #!/usr/bin/env python import commands import subprocess import sys command = commands.getoutput('fs_cli -x "sofia profile external restart"') 这个代码很好用 当我尝试接受参数并传递到命令时,它失败了 command=commands.getoutput(

我正在编写一个python脚本来执行shell命令,我正在获取参数,并希望将该参数的值传递给命令

#!/usr/bin/env python

import commands
import subprocess
import sys

command = commands.getoutput('fs_cli -x "sofia profile external restart"')
这个代码很好用

当我尝试接受参数并传递到命令时,它失败了

command=commands.getoutput('fs_cli-x“sofia profile”+sys.argv[1] +“重新启动”)

支持人员

您应该写:

command = commands.getoutput('fs_cli -x "sofia profile ' + sys.argv[1] + ' restart"')

查看和。

其中一种方法是将要执行的命令转换为字符串,然后作为eval()执行

例如: eval(字符串中的表达式/命令)