How do you use getopts?

落爺英雄遲暮 提交于 2019-12-04 15:20:12
while getopts :xpsr opt; do
   case $opt in
     x ) exit                                ;;
     p ) echo port 10                        ;;
     s ) (( 2 + 2 ))                         ;;
     r ) echo env                            ;;
    \? ) echo "${0##*/}" [ -xpsr ]; exit 1   ;;
  esac
done
Arnaud F.
usage()
{
    echo "Usage: $0 [-o <offset>] [-h];"
    exit 0;
}

# -o (offset) need a value
# -h prints help
offset=0    # 0 is default offset

while getopts o:s opt
do
    case "$opt" in
    d)  offset="$OPTARG";; # changing offset
    s)  usage              # calls function "usage"
    \?) echo "$OPTARG is an unknown option"
        exit 1;; # all other options
    esac
done

shift $((OPTIND-1))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!