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


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

DBA Tips Data Pump Reference

0 comments:

Post a Comment

 

dba topics. Copyright 2011-16 All Rights Reserved | Site Map | Contact | Disclaimer | Google