if statement - how can I do 2 arguments?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if statement - how can I do 2 arguments?
# 1  
Old 04-13-2008
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 Smilie

Micko
# 2  
Old 04-13-2008
Sounds like you want

Code:
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.
# 3  
Old 04-13-2008
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

Code:
	#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

# 4  
Old 04-13-2008
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.

Code:
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.
# 5  
Old 04-13-2008
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.
# 6  
Old 04-13-2008
Try adding double quotes.

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

# 7  
Old 04-16-2008
Quote:
Originally Posted by era
Try adding double quotes.

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


works perfect thankyou
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If statement arguments

I'm stuck on a particular problem and need some guidance. I have a file with a name and a phone number in it (teledir.txt). I need to do a $# in a separate script to take a positional parameter and check to see if it is in the file. To quote the question: If one argument is supplied, check... (6 Replies)
Discussion started by: Eric7giants
6 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

Arguments in usage statement

Hello, I have a question regarding the usage statement of a script. I have 2 parameters "--pto" and "--pto_list". To start the script I will need one of them. Both together are not possible. How this would be printed out within a usage statement? My suggestion would be: Usage:... (4 Replies)
Discussion started by: API
4 Replies

4. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

5. Shell Programming and Scripting

creating printf statement using user arguments

I am writing a script in bash and want to perform the operation I check number of arguments and make a print statement with the passes arguments If I pass 3 arguments I will do printf "$frmt" "$1" "$2" "$3"If I have 4 arguments I do printf "$frmt" "$1" "$2" "$3" "$4"etc (4 Replies)
Discussion started by: kristinu
4 Replies

6. Shell Programming and Scripting

case statement for different cmd arguments

Hello friends, I have a boubt passing different arguments at a time for any one option in below code. I would also like to check which option has been selected (any one of i, r, u ) so that whether or not matching argument passed can be verified. for i and r - install and re-install -... (4 Replies)
Discussion started by: pd2
4 Replies

7. Shell Programming and Scripting

grep with two arguments to arguments to surch for

Hello, is it possible to give grep two documents to surche for? like grep "test" /home/one.txt AND /home/two.txt ? thanks (1 Reply)
Discussion started by: Cybertron
1 Replies

8. Shell Programming and Scripting

Too many arguments?

I can't find anything wrong with this line of code, it works when there is one file in the directory but more than one i get a "too many arguements2 error if ; then am i missing something? (3 Replies)
Discussion started by: Alendrin
3 Replies

9. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

10. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies
Login or Register to Ask a Question