I have a script like
./sample.sh --add employeename -f firstname -a age -d detail
./sample.sh --add employeename -a age -d detail -f firstname
./sample.sh --add employeename -d detail -f firstname
./sample.sh --del employeename
./sample.sh --update employeename -f firstname [age,detail are optional]
here,how could i use getopts for --add, --del, --update and i don't want to use getopt. Good suggestion will be appreciated.
Those are really commands, not options, so should be regular positional arguments:
./sample.sh add employeename -f firstname -a age -d detail
./sample.sh add employeename -a age -d detail -f firstname
./sample.sh add employeename -d detail -f firstname
./sample.sh del employeename
./sample.sh update employeename -f firstname [age,detail are optional]
which should come first; then the remaining options can be parsed using a command-specific set of options.
cmd=$1
shift
case $cmd in
add) do_add "$@" ;;
del) do_del "$@" ;;
update) do_update "$@" ;;
*) echo "Unrecognized command: $cmd"
exit 1
;;
esac
do_add () {
name=$1
shift
while getopts "f:a:d:" opt "$@"; do
...
done
...
}
do_del () {
...
}
# etc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.