Bash脚本中的参数检查问题

Alb*_*elB 5 bash scripting arguments if-statement

所以基本上我试图检查传递给脚本的参数.如果它有三个参数而第三个参数是1,那么我希望它继续.如果它有四个参数且第三个参数不是1,我也希望它继续.

所以基本上我以为我可以做...

if ([ $# -ne 3 ] and [ "$3" -ne "2" ])
then
exit 0
fi
Run Code Online (Sandbox Code Playgroud)

然而,似乎Bash没有和将用于if,所以我认为我可以使用嵌套if,但现在它仍在抱怨.所以这就是我目前所拥有的......

if [ $# -ne 3 ]
then
if [ "$3" -ne "1" ]
then

echo "Improper number of arguments.
FORMAT make-csv-data <STUDY> <TAG> <MODE> <SELECT>

Select can be left off if you want all data (Mode=1)
"
exit 0

fi
fi
if [ $# -ne 4 ]
then
if [ "$3" -ne "2" ]
then

echo "Improper number of arguments.
FORMAT make-csv-data <STUDY> <TAG> <MODE> <SELECT>

Select can be left off if you want all data (Mode=1)
"
exit 0

fi
fi
Run Code Online (Sandbox Code Playgroud)

那我哪里错了?我不能在Bash中嵌套if语句吗?有没有一种超级禅的做法,我完全不知道了?

感谢您给我的任何帮助.


新问题......

现在,由于某种原因,代码根本不起作用.没有错误或任何东西,它只是不起作用.它不检查参数的数量.我根本没有参数运行脚本,它只是跳过它,就像它甚至没有.

奇怪的是,我确信代码昨天正在运行.今天回来,不是那么回事.关于问题是什么的任何想法?(对不起,但我必须删除已接受的答案.)

if [[ $# = 3 && "$3" != "1" ]]
then

echo "Improper number of arguments.
FORMAT make-csv-data <STUDY> <TAG> <MODE> <SELECT>

Select can be omitted if all data is required (Mode=1)
"
exit 0

fi

if [[ $# > 4 ]]
then

echo "Improper number of arguments.
FORMAT make-csv-data <STUDY> <TAG> <MODE> <SELECT>

Select can be omitted if all data is required (Mode=1)
"
exit 0

fi
Run Code Online (Sandbox Code Playgroud)

编辑二:

有一些事情,Bash shell不喜欢这个脚本,我正在尝试做.我可能最终会用另一种脚本语言重写它,并为项目做一些我想到的事情.在任何情况下都感谢您的帮助.

scr*_*gar 19

if [ $# -ne 3 -a "$3" -ne "1" ]; then
  exit 0
fi
Run Code Online (Sandbox Code Playgroud)

以供参考

-a = and
-o = or
Run Code Online (Sandbox Code Playgroud)

或者,你可以使用:

if [[ $# != 3 && "$3" != "1" ]]; then
Run Code Online (Sandbox Code Playgroud)