Using default value with getopts and case

Hey Guys

I have a program in shell which is reading default values from a file

filename: default

MAN=Value1
MANPD=997
REPPD=P1G6

Now the code calling is

#!/bin/sh

. /home/default

while getopts t:D: name
do
 case $name in
  t) TYPE=$OPTARG;;
  D) PDN=${OPTARG:=$MAN};;
esac
done

echo $TYPE
echo $PDN

echo $MAN

So basically if any value to -D is given PDN gets that, else it gets the value of MAN.. now $MAN is giving an output but $PDN (when not set @ runtime) is not setting itself to $MAN..

What am I doing wrong??

while getopts t:D: name

Try

while getopts :tD name

Still no return of

echo $PDN

getopts dosn't support optional : arguments so you will need to quote a blank value eg:

$ yourscript -D "" -t mytype
mytype
Value1
Value1

Or not pass -D at all, and assign PDN=$MAN before calling getops

1 Like

Ookk.. So passing "" works.

I will define PDN before the case and then pass optargs.. if it is empty it will have the default value

thanks :slight_smile: