The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 03-17-2008
gurukottur gurukottur is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 40
option followed by : taking next option if argument missing with getopts

Hi all,

I am parsing command line options using getopts.

The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument.
Below is the script:

while getopts :hd:t:s:l:f: opt
do
case "$opt" in
-h|-\?) usage;;
-d) DEBUG=true;export SCRIPT_LOG_LEVEL=DEBUG ;;
-t) DEVTYPE="$OPTARG" ;;

-s) SIG_IP="$OPTARG"; export SOCKS5_SERVER="$OPTARG:9001" ;;
-l) HOST_LOGIN="$OPTARG";;

-p) PASSWORD="$OPTARG" ;;

-f) cl_cfile_specd=1
export RUNNING_CONFIG_FILE="$OPTARG" ;;

*) logmsg "Unrecognized param usage";;
esac
done
if I run the script as:
$./script.sh -d -t rdsk -s 12.3.4.5 -l guru -p -f /usr/local/sc.conf
the option -p is taking -f as argument.

Please help me how to throw an error and exit the script if mandatory argument is not passed to the script.

Also how to deal with options having -- ie --logfile.

Please help with the above problem