我有以下几点:
if [ ! $# == 2 ] || [ $1 != "mount" -o $1 != "unmount" ]; then例如,对于当前的||运算符,没有任何东西可以通过验证器,但是如果我使用&&运算符,所有的东西都可以通过验证器。
if [ ! $# == 2 ] && [ $1 != "mount" -o $1 != "unmount" ]; then有人能帮我解决这个问题吗?
下面是整个代码块和预期用途
if [ ! $# == 2 ] || [ $1 != "mount" -o $1 != "unmount" ]; then
echo "Usage:"
echo "encmount.sh mount remotepoint # mount the remote file system"
echo "encmount.sh unmount remotepoint # unmount the remote file system"
exit
fi发布于 2013-02-27 16:10:47
你可以这样做:
if [ "$#" -ne 2 ] || [ "$1" != "mount" -a "$1" != "unmount" ]; then
echo "Usage:"
echo "encmount.sh mount remotepoint # mount the remote file system"
echo "encmount.sh unmount remotepoint # unmount the remote file system"
exit -1
fi
echo "OK" 您的测试中有一个小的逻辑错误,因为如果$1不等于"mount"和"unmount",您应该进入usage分支。此外,您还应该使用-eq和-ne运算符(see here)或使用(( ))来比较数字。
请注意,您应该在test ([ ])中引用变量
您也可以像这样组合这两个表达式:
if [ "$#" -ne 2 -o \( "$1" != "mount" -a "$1" != "unmount" \) ]; then如果您有]],还可以使用[[ bash语法:
if [[ $# -ne 2 || ( $1 != "mount" && $1 != "unmount" ) ]]; thenhttps://stackoverflow.com/questions/15106517
复制相似问题