当前位置: 首页 > 面试题库 >

当使用shell = True w / list时,subprocess.call()参数将被忽略

汪才英
2023-03-14
问题内容

我正在尝试获取python的subprocess.call方法以通过列表(由字符串序列组成)接受一些args命令,如python文档中所建议。为了在将此行为放到我的实际脚本中之前对其进行探索,我打开了IPython,运行了一些涉及shell设置和args命令不同组合的命令,并得到以下行为:

In [3]: subprocess.call(['ls', '-%sl' %'a'])
total 320
drwxr-xr-x  20 Kohaugustine  staff   680 Oct 15 16:55 .
drwxr-xr-x   5 Kohaugustine  staff   170 Sep 12 17:16 ..
-rwxr-xr-x   1 Kohaugustine  staff  8544 Oct 15 16:55 a.out
-rwxr-xr-x   1 Kohaugustine  staff  8544 Oct  3 10:28 ex1-6
-rw-r--r--@  1 Kohaugustine  staff   204 Oct  3 10:28 ex1-6.c
-rwxr-xr-x   1 Kohaugustine  staff  8496 Oct  3 10:15 ex1-7
-rw-r--r--@  1 Kohaugustine  staff    71 Oct  3 10:15 ex1-7.c
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:22 hello
-rw-r--r--@  1 Kohaugustine  staff    58 Sep 12 16:27 hello.c
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:24 hello.o
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:24 hello_1.o
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:27 hello_2.o
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:27 hello_3.o
-rwxr-xr-x   1 Kohaugustine  staff  8544 Oct 15 16:55 lesson_1-5
-rw-r--r--@  1 Kohaugustine  staff   185 Sep 28 10:35 lesson_1-5.c
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 21 10:06 temperature.o
-rw-r--r--@  1 Kohaugustine  staff   406 Sep 21 09:54 temperature_ex1-3.c
-rw-r--r--@  1 Kohaugustine  staff   582 Sep 21 10:06 temperature_ex1-4.c
-rw-r--r--@  1 Kohaugustine  staff   178 Sep 23 17:21 temperature_ex1-5.c
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 23 17:21 temperature_ex1-5.o
Out[3]: 0

In [4]: subprocess.call(['ls', '-%sl' %'a'], shell=True)
a.out           ex1-7           hello.c         hello_2.o       lesson_1-5.c            temperature_ex1-4.c
ex1-6           ex1-7.c         hello.o         hello_3.o       temperature.o           temperature_ex1-5.c
ex1-6.c         hello           hello_1.o       lesson_1-5      temperature_ex1-3.c     temperature_ex1-5.o
Out[4]: 0

In [6]: subprocess.call(['ls', '-al'])    
total 320
drwxr-xr-x  20 Kohaugustine  staff   680 Oct 15 16:55 .
drwxr-xr-x   5 Kohaugustine  staff   170 Sep 12 17:16 ..
-rwxr-xr-x   1 Kohaugustine  staff  8544 Oct 15 16:55 a.out
-rwxr-xr-x   1 Kohaugustine  staff  8544 Oct  3 10:28 ex1-6
-rw-r--r--@  1 Kohaugustine  staff   204 Oct  3 10:28 ex1-6.c
-rwxr-xr-x   1 Kohaugustine  staff  8496 Oct  3 10:15 ex1-7
-rw-r--r--@  1 Kohaugustine  staff    71 Oct  3 10:15 ex1-7.c
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:22 hello
-rw-r--r--@  1 Kohaugustine  staff    58 Sep 12 16:27 hello.c
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:24 hello.o
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:24 hello_1.o
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:27 hello_2.o
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:27 hello_3.o
-rwxr-xr-x   1 Kohaugustine  staff  8544 Oct 15 16:55 lesson_1-5
-rw-r--r--@  1 Kohaugustine  staff   185 Sep 28 10:35 lesson_1-5.c
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 21 10:06 temperature.o
-rw-r--r--@  1 Kohaugustine  staff   406 Sep 21 09:54 temperature_ex1-3.c
-rw-r--r--@  1 Kohaugustine  staff   582 Sep 21 10:06 temperature_ex1-4.c
-rw-r--r--@  1 Kohaugustine  staff   178 Sep 23 17:21 temperature_ex1-5.c
-rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 23 17:21 temperature_ex1-5.o
Out[6]: 0

In [7]: subprocess.call(['ls', '-al'], shell = True)
a.out           ex1-7           hello.c         hello_2.o       lesson_1-5.c            temperature_ex1-4.c
ex1-6           ex1-7.c         hello.o         hello_3.o       temperature.o           temperature_ex1-5.c
ex1-6.c         hello           hello_1.o       lesson_1-5      temperature_ex1-3.c     temperature_ex1-5.o
Out[7]: 0

似乎无论何时shell = True,输出似乎都与以下内容相同:

In [9]: subprocess.call(['ls'])
a.out           ex1-7           hello.c         hello_2.o       lesson_1-5.c            temperature_ex1-4.c
ex1-6           ex1-7.c         hello.o         hello_3.o       temperature.o           temperature_ex1-5.c
ex1-6.c         hello           hello_1.o       lesson_1-5      temperature_ex1-3.c     temperature_ex1-5.o
Out[9]: 0

我很困惑;当我设置shell = True时,’-a’选项发生了什么?外壳没读吗?我已经阅读了文档,并说当shell =
True时,这意味着我指定的命令将通过shell执行,因此,这意味着ls
-a被馈送给shell并由shell进行操作。那么,为什么[4]和[7]中的行为呢?pydocs也没有直接解释它(尽管它确实说了当我们将shell设置为False时,什么subpprocess不会做什么)。我们让shell
= False代表什么意思?操作系统中是否产生了一个新进程,而没有外壳实际控制它?

另外,如果我在[3]和[4]中使用格式字符串似乎真的很尴尬,那是因为在我将要使用subprocess.call的实际脚本中,我将不得不依赖这些格式替换为适当命令选项的字符串。我无法对某些命令行选项进行硬编码。使用纯字符串作为args也是不可能的,因为在我的脚本中将有一种方法必须对命令进行列表操作。我不知道是否有更好的方法可以解决此问题,所以如果有人可以提出其他建议,是否真的会有帮助。

非常感谢你!


问题答案:

shell为True时,第一个参数附加到["/bin/sh", "-c"]。如果该参数是一个列表,则结果列表为

["/bin/sh", "-c", "ls", "-al"]

也就是说,仅ls,notls -al用作-c选项的参数。-al用作外壳本身的第一个参数,而不是ls

使用时shell=True,通常只需要传递一个字符串,然后让外壳程序根据外壳程序的常规分词规则对其进行拆分。

# Produces ["/bin/sh", "-c", "ls -al"]
subprocess.call("ls -al", shell=True)

就您而言,似乎根本不需要使用shell=True



 类似资料:
  • 问题内容: 我是Python的初学者,我一直在尝试调用命令行应用程序,但失败了: 但是,如果我添加了所有内容,它们就会开始工作。有人可以解释为什么吗? 问题答案: 您没有使用通话权。查看简介或文档中的任何示例。call的第一个参数是“ args”,这是一个参数序列,其中arg [0]是要运行的程序。 因此,当您执行此操作时: 子流程可以通过两种方式对此进行解释。它应该运行带有参数“ p”,“ i”

  • 问题内容: 如果lambda表达式不需要参数,因此名称过多,该如何写? 这种方式不能编译: 但这确实做到了: 有没有更好的办法? 问题答案: 既然您提到了这可行 我假设预期的功能接口方法必须接受一个参数。自Java8以来,该标识符是保留关键字。 我只会使用一个一次性的(有效标识符)字符。 甚至 明确地说。 在Java语言规范定义了一个lambda表达式的句法 和 换句话说,您不能省略标识符。 作为

  • 问题内容: 当我使用python.exe运行它时,我有一些Python代码可以正常运行,但是如果使用pythonw.exe,则失败。 我收到的消息是: 但是,如果我未指定’stdout’参数,则subprocess.call()会开始正常。 我可以看到pythonw.exe可能正在重定向输出本身,但是我看不到为什么我被阻止为新线程指定stdout。 问题答案: 和handle无效,因为python

  • 我注意到,在Spring中,当声明使用注释的自定义验证器bean时,JSR-303验证被完全忽略。有趣的是,自定义验证器甚至不需要任何类填写或使用。Spring扫描其组件的事实似乎足以让Spring在对象绑定期间跳过JSR-303验证。从自定义验证器中删除,然后重新启动web应用程序,即可按预期启用JSR-303验证。使用注释自定义验证器有其用途,例如使用Spring Autowire依赖项。 考

  • 你好,我在小部件上的以下代码中出错 参数类型'List 这是我的代码 我的代码与上面的图表插件不兼容吗?如何解决这个问题?

  • 问题内容: 在安装了32位python 2.7的64位系统中,我尝试执行以下操作: 但这给了我: 如果我在航站楼里做… …它当然会打印当前的文件夹内容。 我试图将shell参数更改为shell = False。 编辑:实际上,我不能使用调用路径上的任何可执行文件。该语句在另一台机器上运行良好,我认为这是相关的。 如果我做 然后我得到 如果我做: 然后我得到 以上所有操作均在以管理员模式启动的终端中