How to Set / Unset Default Value for Command Line Arguments For a shell script?

advertisements

_____________________________________________________________________________________________________________________

The Eucharistic Miracles of the World

While doing shell scripting you will use command line arguments to pass the values to the program. The program has to verify the value of the command line arguments whether it is having proper values assigned before further execution. Otherwise you can assign a default value for the parameter or argument if applicable.
Syntax:

variable1=${1:-value for the variable} # 1 represent the $1(second argument for the command line
Or
variable1=${1-value for the variable}

To Unset the parameter value
unset  variable1

$ cat tst.sh
echo "Test Program"
log_d=/u01/log
dat_d=/u01/dat
Log_Dir=${1:-$log_d}
echo "Log Directory:" $Log_Dir
Dat_Dir=${2:-$dat_d}
echo "Data Directory:" $Dat_Dir

Output
Without passing the command line value
tst.sh
Test Program
Log Directory: /u01/log
Data Directory: /u01/dat

Passing the command line value

$ tst.sh /u07/log /u07/dat
Test Program
Log Directory: /u07/log
Data Directory: /u07/dat

_____________________________________________________________________________________________________________________

Website Stats

0 comments:

Post a Comment

Labels

Oracle (629) Script (86) General (77) Unix (47) Blog (23) Technology (19) gadget (6) games (6) Business (3) OCI (3) SQL* Loader (3) Datapump (2)
 

acehints.com Copyright 2011-23 All Rights Reserved | Site Map | Contact | Disclaimer