Optional Parameters/arguments while executing a script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optional Parameters/arguments while executing a script.
# 1  
Old 09-03-2010
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 03:40 PM.. Reason: Please use code tags
# 2  
Old 09-03-2010
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.
# 3  
Old 09-03-2010
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
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Positional Parameters Arguments/Variables when using dot (.)

Hi, Is there a special positional variables for when using the dot (.)? Scripts are as below: $: head -100 x.ksh /tmp/y.ksh ==> x.ksh <== #!/bin/ksh # . /tmp/y.ksh 1234 abcd echo "yvar1 = $yvar1" echo "yvar2 = $yvar2" ==> /tmp/y.ksh <== #!/bin/ksh (2 Replies)
Discussion started by: newbie_01
2 Replies

2. UNIX for Beginners Questions & Answers

Extract values based on parameters passing in arguments

Based on arguments passing in command prompt values should fetch and store in new file. Sample:- sh test.sh 10 30 35 45 cat test.sh .. cut -c $1-$2,$3-$4 file_name >> file_new ... ... Above sample passing 4 arguments.. but it may differ (sh test.sh 10 30 35 45 70 75 ) based on... (1 Reply)
Discussion started by: Jairaj
1 Replies

3. Shell Programming and Scripting

Getopts with optional parameters

Hi Unix Gurus, i am on learning path of unix, and yet to discover many things. I came across with this requirement where i need to pass parameters but the position of parameters is not fixed so after doing some google search got to know "getopts" can handle that. So here is my code: function... (3 Replies)
Discussion started by: gnnsprapa
3 Replies

4. Shell Programming and Scripting

How to make shell script arguments optional?

Here is my script: #!/bin/ksh usage () { echo " Usage: $0 <opt1> <opt2> <opt3> <opt4>" } if ; then usage exit; fi prog -a $1 -b $2 -c $3 -d $4 2>&1 | tee -a ~/$1.log I want argument 4 to be optional, so if there's no argument for opt4, that it doesn't... (8 Replies)
Discussion started by: guitarscn
8 Replies

5. Shell Programming and Scripting

Usage: optional and mandatory arguments

I have an awk script which can be used in the following ways: xi and xf will only be mandatory when processing the file fin.zc. awk -v xi=0/-0.5 -v xf=80/30 -f ./zc2cmd.awk fin.zc > fout.cmod awk -f ./zc2cmd.awk -u awk -f ./zc2cmd.awk --usg awk -f ./zc2cmd.awk -e awk -f ./zc2cmd.awk... (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

Two arguments for optional switch

How to declare the two argument for optional switch ? I have a script that search for a string in current or old zipped log file. Im using a option something like this ${basename} Since $1 can have only one argument should be passed when user select swicth -c and -o need to... (3 Replies)
Discussion started by: baraghun
3 Replies

7. Shell Programming and Scripting

Help with executing parallel sessions for same shell script with different but fixed parameters

Hi Experts, There is a shell script that accepts positional parameter between 1-25 to execute case statement of script depending upon the parameter passed. Now I need to run all the 25 sessions parallely. In each option of case statement it is connecting with sqlplus and executing a select... (11 Replies)
Discussion started by: Opamps123
11 Replies

8. Shell Programming and Scripting

tar cmd how many arguments into parameters of filenames

Hi I would like to use tar cmd in my script. I have a variable with filenames, e.g. 1000 records and I would like to paste its values into tar cmd. For this example I used three elements variable strings. strings="file1.txt file2.txt file3.txt" `tar cf file1.tar $strings` Whether... (1 Reply)
Discussion started by: presul
1 Replies

9. Shell Programming and Scripting

How to make parameters optional?

Hi, I am trying to call a function inside a shell script. Is there a way in which I can make the parameters options in the call? Please help me with this. Thanks!!! (2 Replies)
Discussion started by: neeto
2 Replies

10. Answers to Frequently Asked Questions

Passing variables/arguments/parameters to commands

A good place to start is simple variable passing.... Passing variables from one script to another The next level is passing a variable into a more complex command such as using a variable in a sed command. There are some simple quoting techniques that are very general. These are mentioned... (0 Replies)
Discussion started by: Perderabo
0 Replies
Login or Register to Ask a Question