Check for Empty Command Argument | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Check for Empty Command Argument

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-19-2007
Nysif Steve Nysif Steve is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 26 February 2008, 8:36 AM EST
Location: Albany, NY
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Check for Empty Command Argument

I have a script that when called can have 1 or 2 command arguments. If only 1 command argument is passed into the script how can I check that the second argument is null? I am working in Korn shell in a UNIX environment.


Example of script call with 2 arguments:

Code:
% statreport 0300 1430


Example of script call with 1 argument:

Code:
% statreport 0300

Sponsored Links
    #2  
Old 09-19-2007
liketheshell liketheshell is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 30 September 2013, 11:23 AM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Use $# to check for the number of arguments passed. [ -z "$#" ] to check for null arguments.
Sponsored Links
    #3  
Old 09-19-2007
Nysif Steve Nysif Steve is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 26 February 2008, 8:36 AM EST
Location: Albany, NY
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
The user knows to call the script with either 1 or 2 command arguments. Depending on the call I need the script to perform different operations. How exactly would I check if the second command argument is null?
    #4  
Old 09-19-2007
liketheshell liketheshell is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 30 September 2013, 11:23 AM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
You can try doing
if [ "$#" - ne 2 ] then

do something else.

fi

So if the first argument is passed to the script then there is no problem. If the second argument is not supplied then do something else. Hope that helps a bit.
Sponsored Links
    #5  
Old 09-19-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 12 April 2014, 10:59 AM EDT
Location: Toronto, Canada
Posts: 2,875
Thanks: 0
Thanked 108 Times in 101 Posts
Quote:
Originally Posted by Nysif Steve View Post
The user knows to call the script with either 1 or 2 command arguments. Depending on the call I need the script to perform different operations. How exactly would I check if the second command argument is null?

Use test (a.k.a. [):


Code:
if [ -z "$2" ]
then ## Second argument is empty
   : do something
else
   : do something else
fi

Sponsored Links
    #6  
Old 09-19-2007
Nysif Steve Nysif Steve is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 26 February 2008, 8:36 AM EST
Location: Albany, NY
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Still the same error. Here is a larger portion of the code:


Code:
        ((startTime= `perl -e 'print time;'`))
	((currentTime= $startTime))
	((executeTime= $startTime))
	tempTime=`date +%H%M`


	# Check if the second command argument is exists.
	if[ "$#" <  2 ]
	then
		((endTime = $startTime + 28800))		# Add 8 hours = 28800 seconds

	# If The second command line argument isn't empty
	else
		((temp = `expr $2 - $tempTime`))
		if[ $temp < 0 ]					# If temp is negative convert it to positive
		then
			((tempHour = temp / 100))
			((tempHour = tempHour * -3600)) 	# Multiply hours by -3600 seconds per hour to make positive
			((tempMin = temp % 100))
			((tempMin = tempMin * -60))		# Multiple minutes by -60 seconds per minute to make positive
			

		else
			((tempHour = temp / 100))
			((tempHour =  tempHour * 3600)) 	# Multiply hours by 3600 seconds per hour
			((tempMin = temp % 100))
			((tempMin =  tempMin * 60))		# Multiple minutes by 60 seconds per minute
		fi

		((endTime = $endTime + $tempHour + $tempMin))
	fi

Sponsored Links
    #7  
Old 09-19-2007
liketheshell liketheshell is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 30 September 2013, 11:23 AM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
NySif,

Not sure if that is valid shell syntax "if[ "$#" < 2 ]"

Maybe you can try if[ "$#" -lt 2 ]
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Cannot compare argument in if statement in csh/grep command if argument starts with - sarbjit Shell Programming and Scripting 1 02-21-2011 12:23 AM
Empty Directory Check gwrm Shell Programming and Scripting 12 03-01-2010 09:50 AM
How to check if a file is empty? shreekrishnagd UNIX for Dummies Questions & Answers 1 09-09-2008 08:21 AM
How can I make the for command check to see if a file is empty before executing? chrchcol Shell Programming and Scripting 3 07-29-2006 03:14 AM
how to check if the argument contain wildcard (*,?) ? gusla UNIX for Dummies Questions & Answers 3 04-02-2002 05:45 AM



All times are GMT -4. The time now is 08:08 AM.