if statement - how can I do 2 arguments?

I am new to shell, and I am trying to do a if statement like the following:

if [ -z $1 -a -z $2]; then

basically it works fine if both arguments of the if are met, however the next elif is:

elif [ -z $1 ]; then

if the conditions of the elif are met, then it says "final1.sh: line 67: [: too many arguments"

Line 67 is the first if statement.

Basically I am trying to write an "if" that says "if no arguments are there do this.. else if only one of the arguments (specified) is there, do this."

Hope this makes sense :slight_smile:

Micko

Sounds like you want

if [ $# = 0 ]; then
  echo no arguments
elif [ $# = 1 ]; then
  echo one argument "$1"
else ...

Personally I'd prefer case over if for this but that's more of a stylistic issue.

firstly, thankyou for your reply.

I should have included some of my code for you to understand what Im on about..

if I use your method, and I have no arguments it skips the first if statement.
basically i want the first if statement to be "if no searchMonth or searchyear, then"

Cheers for your help

	#if no arguments entered
	if [ $# = 0 ]; then

		echo "Invalid Arguments Entered." 
		echo 
		echo "Usage of this program defined with either of the following inputs:"
		echo "sh programName Month Year"
		echo "sh programName Month"
		echo "sh programName Year"
		echo 
		echo "Full month and year inputs (eg: March 2004) can be used, abbreviation is also a valid input (eg: Mar 04)"
		echo 
		exit
			
	
	elif [ -z $searchMonth ]; then
	
		for (( i=0; i<12; i++))
		do	
			#string comparison
			if [ "$2" = ${month[$i]} -a "$3" = "$searchYear" ]; then
			
				monthCount[$i]=`expr ${monthCount[$i]} + 1`
			
			fi		
				
		done	
		
		print=1	
	
	#if no searchYear entered		
	elif [ -z "$searchYear" ]; then
		
		for (( i=0; i<"${#year[*]}"; i++))
		do		
			#string comparison
			if [ "$2" = "$searchMonth" -a "$3" = "${year[$i]}" ]; then
			
				yearCount[$i]=`expr ${yearCount[$i]} + 1`
				
			fi
			
		done
	print=2

You were using $1 and $2, is that off now?

I'd still use a case statement, because I think it's easier. This gets a bit obscure, though.

case $searchMonth:$searchYear in
  :) echo no arguments;;
  :*) echo year search;;
  *:) echo month search;;
  *) echo both;;
esac

This is not entirely robust in the face of random user input (if they give : as "year" it doesn't work right) but I guess you might be coping with that already earlier in your script.

One reason to prefer case over if is that you don't have to worry about an empty argument string; there are various tricks like if [ X"$input" = X ] but they're rather unsightly IMHO.

hey thanks for the tip, ill try that method out.

Yeah I initially used $1 and $2 just to simplify it, being new to shell I didnt realise hte difference it made.

If still intrigued to the method in which I could get the if statement to accept 2 arguments though.

Try adding double quotes.

if [ -z "$searchMonth" -a -z "$searchYear" ]

works perfect thankyou