bash – Bash脚本错误[:!=:期望一元运算符
问题:
在我的脚本中,我尝试对第一个也是唯一的参数是否等于-v进行错误检查,但这是可选参数。 我使用if语句,但始终收到一元运算符预期的错误。
这是代码:
if [ $1 != -v ]; then
echo "usage: $0 [-v]"
exit
fi
编辑:
我应该更具体一些:上面脚本的这一部分正在检查可选参数,然后,如果未输入该参数,则它将运行程序的其余部分。
#!/bin/bash
if [ "$#" -gt "1" ]; then
echo "usage: $0 [-v]"
exit
fi
if [ "$1" != -v ]; then
echo "usage: $0 [-v]"
exit
fi
if [ "$1" = -v ]; then
echo "`ps -ef | grep -v '\['`"
else
echo "`ps -ef | grep '\[' | grep root`"
fi
In my script I am trying to error check if the first and only argument is equal to -v but it is an optional argument.I use an if statement but I keep getting the unary operator expected error.this is the code:Edit:I should be more specific: This part of the script above is checking an optional argument and then after, if the argument is not entered, it should run the rest of the program.