我试图写这样的python脚本:
import sys
print sys.argv[1]
print sys.argv[2]
让我们称之为arg.py
,并在命令行中运行它:
python arg.py one two
它打印:一两个。
一切都很好。
然后,我希望它可以派上用场,所以我将arg.py放到了自己的计算机中,$PATH
并允许它进行检查,因此无论我在哪里,我都可以简单地arg
在命令行中键入命令来运行此脚本。我试过了
arg one two
但是失败了。异常说:“ bash:测试:one:期望一元运算符”。但是如果我只是做
arg one
工作正常。
我的问题是:为什么我不能像这样传递多个参数?正确的方法是什么?
谢谢!
您可能将脚本命名test
为Bash内置名称。命名其他。
$ help test
test: test [expr]
Evaluate conditional expression.
Exits with a status of 0 (true) or 1 (false) depending on
the evaluation of EXPR. Expressions may be unary or binary. Unary
expressions are often used to examine the status of a file. There
are string operators and numeric comparison operators as well.
The behavior of test depends on the number of arguments. Read the
bash manual page for the complete specification.
...
这就是为什么您从中得到错误的原因bash
:
bash: test: one: unary operator expected
^--------- because it expects an operator to go before 'two'
^-------- and test doesn't like the argument 'one' you've provided
^-------- because it's interpreting your command as the builtin 'test'
^--- Bash is giving you an error
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句