Handling default values when using getopts t::
by Faki from LinuxQuestions.org on (#5T71P)
I have written the following function and want to set a default if user does not supply a value to the option "-t". Haw can I handle this?
Code:myfunc ()
{
local nt ng
local OPTIND OPTARG
local shortopts=":t::g:"
while getopts "$shortopts" arg; do
case $arg in
("t") nt=${OPTARG} ;;
("g") ng=${OPTARG} ;;
(*)
printf "getopts: Error Reporting Mode | Silent "
printf "arg: $arg"
case $arg in
(":")
printf " No value provided to -${OPTARG} option."
printf " Give a value, e.g. \"-${OPTARG} 3\"."
break
;;
("?")
printf "User option not dufined in shortopts."
printf "Invalid -${OPTARG} option provided."
break
;;
esac
fi
break
;;
esac
done
printf '%s\n' "nt: $nt | ng: $ng"
}
Code:myfunc ()
{
local nt ng
local OPTIND OPTARG
local shortopts=":t::g:"
while getopts "$shortopts" arg; do
case $arg in
("t") nt=${OPTARG} ;;
("g") ng=${OPTARG} ;;
(*)
printf "getopts: Error Reporting Mode | Silent "
printf "arg: $arg"
case $arg in
(":")
printf " No value provided to -${OPTARG} option."
printf " Give a value, e.g. \"-${OPTARG} 3\"."
break
;;
("?")
printf "User option not dufined in shortopts."
printf "Invalid -${OPTARG} option provided."
break
;;
esac
fi
break
;;
esac
done
printf '%s\n' "nt: $nt | ng: $ng"
}