Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.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 使用shell=True w/list时忽略subprocess.call()参数_Python_Bash_Shell_Subprocess_Call - Fatal编程技术网

Python 使用shell=True w/list时忽略subprocess.call()参数

Python 使用shell=True w/list时忽略subprocess.call()参数,python,bash,shell,subprocess,call,Python,Bash,Shell,Subprocess,Call,我试图让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:

我试图让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时,子进程将不会做什么);让shell=False表示什么意思?是否在操作系统中生成了一个新进程,而没有由shell实际控制它

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


多谢各位

在列表中使用
shell=True
时,额外的参数将传递给shell本身,而不是传递给shell中运行的命令。然后可以从shell脚本中引用它们(作为
argv[0]
传递),如
$0
$1

最简单的答案是“不要那样做”:如果你想传递一个列表,不要使用
shell=True
;如果要传递字符串,请始终使用
shell=True


这就是说,您可以以读取这些参数的方式形成命令。下面的示例违反了我的上述规则——如果没有bash提供的
shell=True
(和
/bin/sh
),您就无法实现[*],因为它依赖于bash内置版本的
printf
(支持
%q
作为扩展):



[*]-不是真的;可以使用
/bin/bash
作为
argv[0]
shell=False
来更可靠地实现这一点,因为这样它就不再依赖于
/bin/sh
bash

shell=True
和列表一起使用时,额外的参数会传递给shell本身,而不是shell中运行的命令。然后可以从shell脚本中引用它们(作为
argv[0]
传递),如
$0
$1

最简单的答案是“不要那样做”:如果你想传递一个列表,不要使用
shell=True
;如果要传递字符串,请始终使用
shell=True


这就是说,您可以以读取这些参数的方式形成命令。下面的示例违反了我的上述规则——如果没有bash提供的
shell=True
(和
/bin/sh
),您就无法实现[*],因为它依赖于bash内置版本的
printf
(支持
%q
作为扩展):



[*]-不是真的;可以使用
/bin/bash
作为
argv[0]
shell=False
来更可靠地实现这一点,因为它将不再依赖于
/bin/sh
bash

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

["/bin/sh", "-c", "ls", "-al"]
也就是说,只有
ls
,而不是
ls-al
被用作
-c
选项的参数
-al
用作shell本身的第一个参数,而不是
ls

使用
shell=True
时,通常只需传递一个字符串,并让shell根据shell的正常分词规则将其拆分

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

在您的情况下,它看起来根本不需要使用
shell=True

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

["/bin/sh", "-c", "ls", "-al"]
也就是说,只有
ls
,而不是
ls-al
被用作
-c
选项的参数
-al
用作shell本身的第一个参数,而不是
ls

使用
shell=True
时,通常只需传递一个字符串,并让shell根据shell的正常分词规则将其拆分

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

就你而言,它看起来根本不需要使用
shell=True

您可以选择一个文件较少的目录,使示例在此处更紧凑和可读:)相关Python问题:您可以选择一个文件较少的目录,使示例在此处更紧凑和可读:)相关Python问题:谢谢非常地现在这就更有意义了!这应该包括在文档中。非常感谢!现在这就更有意义了!这应该包括在文档中。非常感谢您的帮助!我想我会保持简单,不使用shell=True,但有趣的是,仍然可以使用它,并让shell解释列表中的所有参数。唯一的问题是,它打印出所有的“\”反斜杠字符,这将不好,因为我需要命令在一个由空格分隔的连续行中。谢谢