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
Calling sql in shell script with parameters Radhe Shell Programming and Scripting 2 04-21-2008 09:27 AM
How to pass two or more parameters to the main in shell script pinky UNIX for Dummies Questions & Answers 0 10-12-2007 08:54 AM
Call a UNIX shell with parameters from C dustman UNIX for Dummies Questions & Answers 1 03-14-2007 07:26 PM
Max number of parameters to korn shell? rajus19 Shell Programming and Scripting 4 02-03-2006 06:01 AM
Number of parameters to a shell script videsh77 Shell Programming and Scripting 1 05-13-2005 07:39 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 12-28-2001
Registered User
 

Join Date: Nov 2001
Posts: 20
Unhappy Shell parameters

I need to check the number of input parameters in the K shell script.
Also, I want to exit if the no of parameters is not met.


How can I do that ?

Thanks
LS1429
Forum Sponsor
  #2  
Old 12-28-2001
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
I think I see what you mean... For example, the command:
myscript.ksh -a 1 -q
has 3 input parameters... Am I correct? If so, you can use $# to count them. Here is an example:
Code:
#!/usr/bin/ksh
 if [ "$#" = "0" ]; then
     echo "You don't have any arguments! "; exit
fi
echo $#
To try this script, try this:
chmod +x scriptname
./scriptname
./scriptname 1 b 3 d
See what happens.

If you have any more questions, please feel free to post back. I hope I got what you were asking.
  #3  
Old 12-28-2001
Registered User
 

Join Date: Nov 2001
Posts: 20
Shell Parameters

Yeah thatz it.
One more thing. But I am not clear why the ; after condition is really needed or not

if [ $# -ne 1 ]
then
echo "Provide one Parameter "
exit 1
fi

Thanks
LS1429
  #4  
Old 12-28-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,619
Re: Shell Parameters

Quote:
Originally posted by ls1429
Yeah thatz it.
One more thing. But I am not clear why the ; after condition is really needed or not
Take an easier example, the date and who commands. If we want to run both commands, we might use two lines:
date
who
or we can enter both commands on a single line if we use the semicolon like this:
date ; who
But we gotta have something separating the two commands. Usually we have the newline character as the delimiter between commands, but the semicolon also works.

So it's a matter of style whether you use
if [ ... ] ; then
or
if [ ... ]
then

I like the first style because it lets more of the script be visible on a page. But I wouldn't call the second style "wrong".
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0