Each long option name in longopts may be followed by one colon to indicate it has a required argument, and by two colons to indicate it has an optional argument.
in man bash
.
I wrote a bash function optionarg
:
optionarg (){
opts=$(getopt -o , --long name:,pass:: -- "$@")
eval set -- "$opts"
while true; do
case "$1" in
--name)
name=$2
echo "name is " $name
shift 2;;
--pass)
case "$2" in
'')
echo "no pass argument"
shift 2;;
*)
pass=$2
echo "pass" $pass
shift 2;;
esac;;
--)
shift 1;;
*) break;;
esac
done }
Let test my optionarg here.
1.Assign no argument with pass
.
optionarg --name tom --pass
name is tom
no pass argument
2.Assign an argument xxxx
with pass
.
optionarg --name tom --pass xxxx
name is tom
no pass argument
How to fix my optionarg
function to get such output?
optionarg --name tom --pass xxxx
name is tom
pass xxxx