在子进程命令中传递用双引号包裹的参数 [英] Passing arguments wrapped with double quotes in a subprocess command

查看:26
本文介绍了在子进程命令中传递用双引号包裹的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 subprocess.check_output() 方法从 python 脚本中执行命令.有一些命令需要 "(双引号)出现在语法中.

I am using subprocess.check_output() method to execute commands from within the python script. There are some commands that need " (double quotes) to be present in syntax.

这是一个例子:

> drozer console connect -c "run app.package.info -a com.package.name"

如果我们从上面的命令中删除 "(双引号),它会抛出错误.

It throws error if we remove " (double quotes) from above command.

我做了以下事情:

string = '\"run app.package.info -a com.package.name\"'
command = ['/usr/bin/drozer','console','connect','-c',string]
output = subprocess.check_output(command)

这让我出错:

*** Unknown syntax: "run app.package.info  -a com.package.name"

如何解决这个引号问题?

How can I solve this issue of quotes?

推荐答案

你不需要双引号.

您在 shell 命令中需要它们的原因是 shell 正在从字符串解析命令行,并将它们用作文本 run app.package.info -a com.package 的指示.name 应该放在一个 single 参数中:

The reason you need them with the shell command is that the shell is parsing the command line from a string, and it uses them as an indication that the text run app.package.info -a com.package.name should be placed in a single argument:

#!/usr/bin/env python
import subprocess

c_arg = 'run app.package.info -a com.package.name'
command = ['/usr/bin/drozer', 'console', 'connect', '-c', c_arg]
output = subprocess.check_output(command)
print("Got %r" % (output,))

当您使用代码启动进程时,您明确指定了各个参数,并且不会进行 shell 解析,因此不需要额外的引号.

When you’re using code to start a process, you explicitly specify individual arguments, and no shell parsing is going on, so there’s no need for the extra quotes.

这篇关于在子进程命令中传递用双引号包裹的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆