So basically I am trying to check the arguments that are passed into the script. If it has three arguments and the third argument is a 1, then I want it to continue. I also want it to continue if it has four arguments and the third argument is not a 1.
所以基本上我试图检查传递给脚本的参数。如果它有三个参数而第三个参数是1,那么我希望它继续。如果它有四个参数且第三个参数不是1,我也希望它继续。
So basically I thought that I could just do...
所以基本上我以为我可以做...
if ([ $# -ne 3 ] and [ "$3" -ne "2" ])
then
exit 0
fi
However it seems that Bash does not have and's to use for if's,so then I figured that I could just use nested if's, however now it's complaining still. So this is what I have currently...
然而,似乎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
So where am I going wrong? Can I not nest if statements in Bash? Is there a super-zen way of doing this that I'm missing altogether?
那我哪里错了?我不能在Bash中嵌套if语句吗?这样做有一种超级禅的方式,我完全不知道了吗?
Thanks for the any help you could give me.
感谢您给我的任何帮助。
New Problem...
Now, for some reason or another, the code isn't working at all. There are no errors or anything, it just doesn't work. It doesn't check the number of arguments. I've run the script with no arguments at all and it just skips it like it's not even there.
现在,由于某种原因,代码根本不起作用。没有错误或任何东西,它只是不起作用。它不检查参数的数量。我根本没有参数运行脚本,它只是跳过它,就像它甚至没有。
Weird part is that I was sure that the code was working yesterday. Come back today, not so much. Any ideas on what the problem is? (Sorry, but I have to remove the accepted answer on this.)
奇怪的是,我确信代码昨天正常运行。今天回来,不是那么回事。关于问题是什么的任何想法? (对不起,但我必须删除已接受的答案。)
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
EDIT II:
There are a few things that the Bash shell isn't liking about this script that I'm trying to do. I'll probably end up rewriting it in another scripting language and do a few more things that I have in mind for the project. Thanks for the help in any case.
有一些事情是Bash shell不喜欢我正在尝试做的这个脚本。我可能最终会用另一种脚本语言重写它,并为项目做一些我想到的事情。在任何情况下都感谢您的帮助。
2 个解决方案
#1
if [ $# -ne 3 -a "$3" -ne "1" ]; then
exit 0
fi
For reference
-a = and
-o = or
Or, you could just use use:
或者,你可以使用:
if [[ $# != 3 && "$3" != "1" ]]; then
#2
Please see:
http://bash-hackers.org/wiki/doku.php/commands/classictest#and_and_or and http://bash-hackers.org/wiki/doku.php/syntax/ccmd/conditional_expression
http://bash-hackers.org/wiki/doku.php/commands/classictest#and_and_or和http://bash-hackers.org/wiki/doku.php/syntax/ccmd/conditional_expression
Since you're just checking exit/return values with "if", you need to provide something, e.g. a command, that provides meaningful ones based on your tests. [ is such a command, another possibility is the [[ keyword.
由于您只是使用“if”检查退出/返回值,因此您需要提供一些内容,例如一个命令,根据您的测试提供有意义的命令。 [是这样的命令,另一种可能性是[[关键字。
The actual correct examples already were mentioned by scragar above, I don't want to just repeat them :)
上面的scragar已经提到了实际正确的例子,我不想重复它们:)
#1
if [ $# -ne 3 -a "$3" -ne "1" ]; then
exit 0
fi
For reference
-a = and
-o = or
Or, you could just use use:
或者,你可以使用:
if [[ $# != 3 && "$3" != "1" ]]; then
#2
Please see:
http://bash-hackers.org/wiki/doku.php/commands/classictest#and_and_or and http://bash-hackers.org/wiki/doku.php/syntax/ccmd/conditional_expression
http://bash-hackers.org/wiki/doku.php/commands/classictest#and_and_or和http://bash-hackers.org/wiki/doku.php/syntax/ccmd/conditional_expression
Since you're just checking exit/return values with "if", you need to provide something, e.g. a command, that provides meaningful ones based on your tests. [ is such a command, another possibility is the [[ keyword.
由于您只是使用“if”检查退出/返回值,因此您需要提供一些内容,例如一个命令,根据您的测试提供有意义的命令。 [是这样的命令,另一种可能性是[[关键字。
The actual correct examples already were mentioned by scragar above, I don't want to just repeat them :)
上面的scragar已经提到了实际正确的例子,我不想重复它们:)