views:

1330

answers:

2

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.

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...

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?

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.

+7  A: 
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
scragar
Thanks for the help. This helped me out on a couple other scripts I was working on as well.
Dropped.on.Japan
+1  A: 

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

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.

The actual correct examples already were mentioned by scragar above, I don't want to just repeat them :)

TheBonsai