我试图写一个像这样的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:test:one:一元运算符预期”。但是,如果我只是做
arg one
它运作良好。
我的问题是:为什么我不能传递这样的多个参数?什么是正确的方法?
谢谢!
答案 0 :(得分:4)
您可能将脚本命名为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
答案 1 :(得分:0)