TransWikia.com

case while option not showing last argument in shell script

Unix & Linux Asked by user2358844 on December 19, 2020

Shell script is not showing last value in getopts using while command. See below command and code and output

command: nohup ksh newtome.ksh -m 100 -l LSD -t 10202020 -p ABC,CDE > log.txt &

masterLog="/testing/log/jlog123.txt"

if [ $# -lt 8 ]; then
   echo "Usage: $0 -m ab -l cd -t ef -p gh"
   echo "Usage: $0 -m ab -l cd -t ef -p gh" >> $masterLog
   exit 1
fi


while getopts m:l:t:p option
do
       case ${option} in
        m) if [[ ${OPTARG} = -* ]]; then
                echo "Invalid parameter "${OPTARG}" provided for argurment "-${option}""
                echo "Invalid parameter "${OPTARG}" provided for argurment "-${option}"" > $masterLog
                exit 1;
           fi
           ab=$OPTARG;;
        l) if [[ ${OPTARG} = -* ]]; then
                echo "Invalid parameter "${OPTARG}" provided for argurment "-${option}""
                echo "Invalid parameter "${OPTARG}" provided for argurment "-${option}"" > $masterLog
                exit 1;
           fi
          cd=$OPTARG;;
        t) if [[ ${OPTARG} = -* ]]; then
                echo "Invalid parameter "${OPTARG}" provided for argurment "-${option}""
                echo "Invalid parameter "${OPTARG}" provided for argurment "-${option}"" > $masterLog
                exit 1;
           fi
           ef=$OPTARG;;
        p) if [[ ${OPTARG} = -* ]]; then
                echo "Invalid parameter "${OPTARG}" provided for argurment "-${option}""
                echo "Invalid parameter "${OPTARG}" provided for argurment "-${option}"" > $masterLog
                exit 1;
           fi
           gh=$OPTARG;;
        ?)  print "Usage: $0 -p password -i ds" > $masterLog
             print >&2 "echo "Usage: $0 -m ab -l cd -t ef -p gh""
             exit 1;;
        esac
done

OUTPUT:

+ getopts m:l:t:p option
+ ab=100
+ getopts m:l:t:p option
+ cd=LSD
+ getopts m:l:t:p option
+ ef=10202020
+ getopts m:l:t:p option
+ gh=
+ getopts m:l:t:p option

One Answer

The:

        p) if [[ ${OPTARG} = -* ]]; then
                echo "Invalid parameter "${OPTARG}" provided for argurment "-${option}""
                echo "Invalid parameter "${OPTARG}" provided for argurment "-${option}"" > $masterLog
                exit 1;
           fi
           gh=$OPTARG;;

Suggests the p option requires an argument, so your getopts option specification should have a : after the p: getopts m:l:t:p: option.

Without it, the -p option is handled as an argument-less option.

Why are you prohibiting option arguments starting with - btw?

Also note that echo can't be used for arbitrary data, use printf '%sn' ... or print -r -- .... And in ksh88, expansions in targets of redirections must be quoted. More generally, you'll want to quote every expansion to be on the safe side.

Errors should go on stderr, not stdout. So print -ru2 -- "Invalid...", though here, since you're printing the errors twice every time, you may want to make it a function to factorise code a bit:

exec 3>> "$masterlog"
function error {
  print -ru2 -- "$@"
  print -ru3 -- "$@"
}

# ...

error "Invalid..."

Or even a specific function to reject option arguments starting with - since you're doing it several times in the script.

Answered by Stéphane Chazelas on December 19, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP