Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux bash脚本中的参数数_Linux_Bash_Shell_Command Line Arguments - Fatal编程技术网

Linux bash脚本中的参数数

Linux bash脚本中的参数数,linux,bash,shell,command-line-arguments,Linux,Bash,Shell,Command Line Arguments,我想测试传递给Linux shell脚本的参数数量。如果参数的数量不是2或4,它应该打印一些内容。不幸的是,它不起作用。谁能解释我做错了什么 #!/bin/bash if [[ $# -ne 2 ]] || [[ $# -ne 4 ]]; then echo "here"; fi 逻辑 逻辑 您应该将逻辑或替换为逻辑和,因此: #!/bin/bash if [[ $# -ne 2 && $# -ne 4 ]]; then echo "here" fi 以算术形

我想测试传递给Linux shell脚本的参数数量。如果参数的数量不是
2
4
,它应该打印一些内容。不幸的是,它不起作用。谁能解释我做错了什么

#!/bin/bash
if [[ $# -ne 2 ]] || [[ $# -ne 4 ]];
then
    echo "here";
fi
逻辑

逻辑


您应该将逻辑
替换为逻辑
,因此:

#!/bin/bash

if [[ $# -ne 2 && $# -ne 4 ]]; then
   echo "here"
fi
以算术形式:

#!/bin/bash

if (($# != 2 && $# != 4)); then
   echo "here"
fi

如您所见,无需使用2
[[]]

您应该用logical
替换logical
,因此:

#!/bin/bash

if [[ $# -ne 2 && $# -ne 4 ]]; then
   echo "here"
fi
以算术形式:

#!/bin/bash

if (($# != 2 && $# != 4)); then
   echo "here"
fi

正如您所看到的,不需要使用2
[[[]]

它的
-ne
(不相等)的可能重复。同样,OP发布的代码也有一个问题:值始终不是2或不是4它的
-ne
(不相等)同样,OP发布的代码也有一个问题:值始终不是2或不是4