Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用以下代码在 OSX 下比较 bash 中的参数...
#!/bin/bash if ["$1" == "1"] then echo $1 else echo "no" fi
但我不断收到以下错误
$bash script.sh 1 script.sh: line 3: [1: command not found no
如何阻止它尝试评估“1”?
[是一个测试命令,所以你需要在 and 之间有[一个"$1"空格,以及在"1"和结束之间有一个空格]
[
"$1"
"1"
]
编辑
只是为了澄清一下,需要空格是因为bash 命令[的语法不同test,所以以下是编写脚本的另一种方式:
test
#!/bin/bash if test "$1" == "1" then echo $1 else echo "no" fi
可以进一步简化为
#!/bin/bash [ "$1" == "1" ] && echo "$1" || echo "no"