while [ $# -gt 0 ]; do
case $1 in
-h|--help)
print_help
exit 0
;;
-V|--version)
print_version
exit 0
;;
-l|--listen)
listening_address=$2
shift 2
;;
-p|--port)
lipo=$2
shift 2
;;
-P|--protocol)
proto=$2
shift 2
;;
-v|--verbose)
verbose=true
shift
;;
--)
shift
break
;;
*)
echo "Internal Error: option processing error: $1" 1>&2
exit 1
;;
esac
done
看一个shell源码时学习到的,用shift 命令归位当前长度,然后做while循环,很有技巧。
本文介绍了一段Shell脚本中使用shift命令处理命令行参数的技巧,通过while循环和case语句实现了对多种选项的有效解析,包括帮助信息、监听地址、端口等配置。
9177

被折叠的 条评论
为什么被折叠?



