[英]pass variable in to command in python
i'm writing a python script to execute shell command, and i'm taking arguments and i want to pass the value of that to the command我正在编写一个 python 脚本来执行 shell 命令,我正在接受参数,我想将它的值传递给命令
#!/usr/bin/env python
import commands
import subprocess
import sys
command = commands.getoutput('fs_cli -x "sofia profile external restart"')
this code works fine这段代码工作正常
when i try to take the argument and pass to command it fails当我尝试接受参数并传递给命令时它失败了
command = commands.getoutput('fs_cli -x "sofia profile" + sys.argv[1] + " restart"') command = commands.getoutput('fs_cli -x "sofia profile" + sys.argv[1] + "restart"')
supp folks给伙计们
你应该写:
command = commands.getoutput('fs_cli -x "sofia profile ' + sys.argv[1] + ' restart"')
看看argparse和subprocess 。
One of the way to do this is to convert your command that you want to execute into string and then execute it as eval()一种方法是将您要执行的命令转换为字符串,然后将其作为 eval() 执行
example : eval(expression/command in string)示例:eval(字符串中的表达式/命令)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.