I want to test the number of arguments passed to a Linux shell script. If the number of arguments is not 2
or 4
, it should print something. Unfortunately it does not work. Can anyone explain what I am doing wrong?
我想测试传递给Linux shell脚本的参数数量。如果参数的数量不是2或4,它应该打印一些东西。不幸的是,它不起作用。有人能解释一下我做错了什么吗?
#!/bin/bash
if [[ $# -ne 2 ]] || [[ $# -ne 4 ]];
then
echo "here";
fi
2 个解决方案
#1
2
You should replace logical OR
by logical AND
, so :
你应该用逻辑或逻辑来代替逻辑,所以:
#!/bin/bash
if [[ $# -ne 2 && $# -ne 4 ]]; then
echo "here"
fi
In arithmetic form:
在算术形式:
#!/bin/bash
if (($# != 2 && $# != 4)); then
echo "here"
fi
As you can see, no need to use 2 [[ ]]
如您所见,不需要使用2 []
#2
1
Logic.
逻辑。
if [[ $# -ne 2 ]] && [[ $# -ne 4 ]]; then
echo "here"
fi
#1
2
You should replace logical OR
by logical AND
, so :
你应该用逻辑或逻辑来代替逻辑,所以:
#!/bin/bash
if [[ $# -ne 2 && $# -ne 4 ]]; then
echo "here"
fi
In arithmetic form:
在算术形式:
#!/bin/bash
if (($# != 2 && $# != 4)); then
echo "here"
fi
As you can see, no need to use 2 [[ ]]
如您所见,不需要使用2 []
#2
1
Logic.
逻辑。
if [[ $# -ne 2 ]] && [[ $# -ne 4 ]]; then
echo "here"
fi