Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument Checking Problem in Bash Script

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.

like image 485
AlbertEngelB Avatar asked May 26 '09 15:05

AlbertEngelB


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

How do you check if an argument is a file in bash?

In order to check if a file exists in Bash, you have to use the “-f” option (for file) and specify the file that you want to check.

What's do `` $() ${} commands do in bash?

$() – the command substitution. ${} – the parameter substitution/variable expansion.


2 Answers

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
like image 87
scragar Avatar answered Oct 10 '22 04:10

scragar


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 :)

like image 23
TheBonsai Avatar answered Oct 10 '22 03:10

TheBonsai