The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check if file is empty stolz Shell Programming and Scripting 8 03-22-2009 11:15 PM
Check for empty string rahman_riyaz Shell Programming and Scripting 12 01-24-2008 03:13 AM
How to check for null or empty string doer Shell Programming and Scripting 5 07-24-2007 02:31 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 04:14 AM
how to check if the argument contain wildcard (*,?) ? gusla UNIX for Dummies Questions & Answers 3 04-02-2002 07:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-19-2007
Nysif Steve Nysif Steve is offline
Registered User
  
 

Join Date: Aug 2007
Location: Albany, NY
Posts: 26
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

  #2 (permalink)  
Old 09-19-2007
liketheshell liketheshell is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 21
Use $# to check for the number of arguments passed. [ -z "$#" ] to check for null arguments.
  #3 (permalink)  
Old 09-19-2007
Nysif Steve Nysif Steve is offline
Registered User
  
 

Join Date: Aug 2007
Location: Albany, NY
Posts: 26
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 (permalink)  
Old 09-19-2007
liketheshell liketheshell is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 21
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.
  #5 (permalink)  
Old 09-19-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
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

  #6 (permalink)  
Old 09-19-2007
Nysif Steve Nysif Steve is offline
Registered User
  
 

Join Date: Aug 2007
Location: Albany, NY
Posts: 26
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

  #7 (permalink)  
Old 09-19-2007
liketheshell liketheshell is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 21
NySif,

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

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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0