How to make shell script arguments optional?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make shell script arguments optional?
# 1  
Old 12-31-2011
How to make shell script arguments optional?

Here is my script:

Code:
#!/bin/ksh

usage ()
{
     echo " Usage: $0 <opt1> <opt2> <opt3> <opt4>"
}

if [ $# -lt 3 ]; 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 execute the -d argument in the actual program called 'prog'
# 2  
Old 12-31-2011
Try:
Code:
#!/bin/ksh

usage ()
{
     echo " Usage: $0 <opt1> <opt2> <opt3> <opt4>"
}

if [ $# -lt 3 ]; then
        usage
        exit;
fi

if [ -z $4 ]; then
  prog -a $1 -b $2 -c $3 2>&1 | tee -a ~/$1.log
else
  prog -a $1 -b $2 -c $3 -d $4 2>&1 | tee -a ~/$1.log
fi

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 12-31-2011
Or the shorter:

Code:
#!/bin/ksh

usage ()
{
     echo " Usage: $0 <opt1> <opt2> <opt3> [<opt4>]"
}

[[ $# -lt 3 ]] && { usage; exit; }

prog -a $1 -b $2 -c $3 ${4:+-d $4} 2>&1 | tee -a ~/$1.log

This User Gave Thanks to jlliagre For This Post:
# 4  
Old 12-31-2011
@bartus11
Would it be safer to put double quotes around the $4?:
Code:
...
if [ -z "$4" ]; then
...

@jlliagre
What exactly does "${4:+-d $4}" mean? Can this be applied to the other arguments as well, to make them optional?

Thanks
# 5  
Old 12-31-2011
Quote:
Originally Posted by guitarscn
What exactly does "${4:+-d $4}" mean?
From ksh manual page (but this applies to all posix shells):
Code:
${parameter:+word}

       If  parameter  is  set  and is non-null, substitute word. Otherwise
       substitute nothing.

Here, "word" is "-d $4"
Quote:
Can this be applied to the other arguments as well, to make them optional?
Yes.
# 6  
Old 01-01-2012
This would require at least 3 arguments though, correct? Because if you only provided 2, the script wouldn't know which 2 arguments they are for and most likely use the first two options. Right? Or can this be circumvented somehow?
# 7  
Old 01-01-2012
Here is generic solution. Argument order is not fixed and so on.
Code:
usage ()
{
     echo " Usage: $0 -a value -b value -c value [ -d value ]" >&2
     exit 1
}

#####################
a=""
b=""
c=""
d=""
# parse options
while [ $# -gt 0 ]
do
        case "$1" in
                -a) a="$2" ; shift ;;
                -b) b="$2" ; shift ;;
                -c) c="$2" ; shift ;;
                -d) d="$2" ; shift ;;
                --) shift; break ;;
                -*) usage ;;
                *) break ;;  # arguments ...
        esac
        shift
done
# now $* include arguments
[ "$a" = "" ] && usage
[ "$b" = "" ] && usage
[ "$c" = "" ] && usage

if [ "$d" = "" ] ; then
      prog -a "$a" -b "$b" -c "$c" 2>&1 | tee -a ~/"$a".log 
else
     prog -a "$a" -b "$b" -c "$c" -d "$d" 2>&1 | tee -a ~/"$a".log
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash shell script with mandatory and optional input

Hello I would like to write a bash shell script which will need user to supply one variable which is mandatory and some other optional variables. If mandatory variable is not supplied by user, the script will exit. If optional values are not supplied by user, hard-coded value (in the script)... (3 Replies)
Discussion started by: atanubanerji
3 Replies

2. Shell Programming and Scripting

To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command > ssh -qtt user@host < test.sh However, when I try to pass arguments to test.sh it fails. Any pointers would be appreciated. (7 Replies)
Discussion started by: Sree10
7 Replies

3. Shell Programming and Scripting

sub arguments to shell script

Hi, I have a shell script, when run it i get a prompt to enter arguments say 1 for doing my next task otherwise q for quit. What I am trying to do is run the shell script with the argument passed in however it does not seem to work. This is what I did ./test.sh 1 Instead it printed the line... (6 Replies)
Discussion started by: aqua9
6 Replies

4. Shell Programming and Scripting

Using arguments in Shell script

Hello, I have to make a shell script doing that : the program tests if there is an argument, if there is it checks whether this is a directory or not, If it is it opens it. for any .c file in the directory it prints 2 lines in the screen : the dependence line of the .o and compiler commend... (1 Reply)
Discussion started by: dekl
1 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

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 ./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... (3 Replies)
Discussion started by: indrajit_u
3 Replies

8. Shell Programming and Scripting

How to make 2 separate arguments in 1 bash script?

This is what I have: #!/bin/bash #ascript.sh WORD1=`tail -n +$1 /home/gscn/word1.txt | head -1` sed -e "s/WORD1/$WORD1/g" < /home/gscn/configtmp > /home/gscn/config WORD2=`tail -n +$1 /home/gscn/word2.txt | head -1` sed -e "s/WORD2/$WORD2/g" < /home/gscn/config2tmp >... (4 Replies)
Discussion started by: guitarscn
4 Replies

9. Homework & Coursework Questions

I need to make a script that has 4 command line arguments to function properly.

I have no idea what the following means. The teacher is too advanced for me to understand fully. We literally went from running a few commands over the last few months to starting shell scripting. I am not a programmer, I am more hardware oriented. I wish I knew what this question was asking... (3 Replies)
Discussion started by: Wookard
3 Replies

10. 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
Login or Register to Ask a Question