[英]Bash not recognising python command
I have this command as part of a bash script 我将此命令作为bash脚本的一部分
$(python -c "import urllib, sys; print urllib.unquote(sys.argv[0])", "h%23g")
But when I run it, I get this: 但是当我运行它时,我得到了:
-bash: -c: command not found
As though bash has missed reading the python
, and is thinking -c
is the name of the command. 好像bash错过了阅读
python
想法,并认为-c
是命令的名称。 Exactly the same happens when using backticks. 使用反引号时完全相同。
How can I make bash recognise the python
? 如何使bash识别
python
?
the Python command is returning the string "-c" from your $(...) structure, which bash then tries to execute. Python命令从$(...)结构返回字符串“ -c”,然后bash尝试执行。
for example 例如
python -c "import urllib, sys; print urllib.unquote(sys.argv[0])"
prints "-c", so you are essentially asking bash to interpret $(-c), for which the error is valid. 打印“ -c”,因此您实质上是在要求bash解释错误有效的$(-c)。
I think you want something like the following: 我认为您需要以下内容:
$(python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" "h%23g")
This will result in h#g
, if this is all you have on a line then it will also attempt to run a command called h#g
, so I'm assuming you are actually using this as a part of a larger command. 这将导致
h#g
,如果这是您在线上的全部内容,那么它还将尝试运行名为h#g
的命令,因此我假设您实际上是将其用作较大命令的一部分。
The issue with your version is that sys.argv[0]
is the -c
from the command, and urllib.unquote('-c')
will just return '-c'
. 您的版本的问题是
sys.argv[0]
是命令中的-c
,而urllib.unquote('-c')
只会返回'-c'
。
From the documentation on sys.argv
: 从
sys.argv
的文档中 :
If the command was executed using the
-c
command line option to the interpreter,argv[0]
is set to the string'-c'
.如果命令是使用解释器的
-c
命令行选项执行的,则argv[0]
设置为字符串'-c'
。
Combining that with info from the man page (emphasis mine): 结合手册页中的信息 (重点是我的):
-c command
-c 命令
Specify the command to execute (see next section).指定要执行的命令(请参阅下一节)。 This terminates the option list (following options are passed as arguments to the command) .
这将终止选项列表(以下选项作为参数传递给命令) 。
So, when you use -c
, sys.argv[0]
will be '-c'
, the argument provided to -c
is the script so it will not be included in sys.argv
, and any additional arguments are added to sys.argv
starting at index 1. 因此,当您使用
-c
, sys.argv[0]
将为'-c'
,提供给-c
的参数是脚本,因此它将不会包含在sys.argv
,并且会将任何其他参数添加到sys.argv
从索引1开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.