Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-03-2010
Registered User
 

Join Date: Jun 2010
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
Optional Parameters/arguments while executing a script.

Hello,

I have a shell script "Test.ksh" and I need to pass 8 parameters/arguments while executing the script


Code:
./Test.ksh 1 2 3 4 5 6 7 8

Out of these I want first 3 to be compulsory and rest 5 to be optional. Can you suggest the way to do this like and also how to pass these optional arguments like

Is it

Code:
./Test.ksh 1 2 3 4 7 8 #OR...
./Test.ksh 1 2 3 4 <space> <space>  7 8 or any other???


Last edited by Scott; 09-03-2010 at 02:40 PM.. Reason: Please use code tags
Sponsored Links
    #2  
Old 09-03-2010
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 

Join Date: Jun 2009
Location: Switzerland - ZH
Posts: 5,352
Thanks: 130
Thanked 535 Times in 473 Posts
Hi.


Code:
if [ $# -lt 3 ]; then
  echo "not enough arguments..."
  ...
fi

You can have as many spaces as you like between arguments.

I would suggest looking at getopts.
Sponsored Links
    #3  
Old 09-03-2010
Registered User
 

Join Date: Jun 2010
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
Hello..

Thanks for the quicky..
Will "if [ $# -lt 3 ]" check for case when any 3 arguments are passed or the first 3.
    #4  
Old 09-03-2010
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 

Join Date: Jun 2009
Location: Switzerland - ZH
Posts: 5,352
Thanks: 130
Thanked 535 Times in 473 Posts
Hi.

It will check that three arguments are passed.

It will not tell you if arguments 1, 2 and 3, or 1, 4 an 7 are passed, as you think of them.

I would suggest you use getopts.
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
executing perl with arguments in tcl sukrish Shell Programming and Scripting 0 08-24-2010 04:49 AM
tar cmd how many arguments into parameters of filenames presul Shell Programming and Scripting 1 06-15-2010 05:02 AM
How to make parameters optional? neeto Shell Programming and Scripting 2 05-23-2008 11:57 PM
help me in sending parameters from sqlplus script to unix shell script Hara Shell Programming and Scripting 2 01-29-2008 02:31 PM
Passing variables/arguments/parameters to commands Perderabo Answers to Frequently Asked Questions 0 06-13-2004 07:37 PM



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