bash - shift 和 getopts
问题描述
我正在使用getopts
并打算使用-v
可以采用可选参数的选项。
当使用getopt
isstead of 时getopts
,人们习惯使用shift
命令转到下一个参数。但我没有看到人们在使用shift
时使用getopts
.
local vb=1 sort=0
local OPTIND OPTARG
local shortopts="Vuhvs"
while getopts $shortopts arg; do
case $arg in
("V") printf '%s\n' "Version" ; return ;;
("u") printf '%s\n' "usage" ; return ;;
("h") printf '%s\n' "help" ; return ;;
("v")
# Allows argument to be optional.
# Defines option with no arguments in shortopts.
nextarg=${!OPTIND}
if [[ (-n "$nextarg") && ("$nextarg" != -*) ]] ; then
OPTIND=$((OPTIND + 1))
vb="$nextarg"
fi
;;
#.............................
("s") sort=1 ; shift ;; # shifts arg by 1
#.............................
(?)
pfm "Invalid option: -${OPTARG}."
pfm "Invoke \`linge-pfa -h\` for details."
break
;;
esac
done
解决方案
推荐阅读
- reactjs - 使用 useSelector 钩子将每个子 React 组件的状态分开
- c++ - 穿山甲 - s_cam vs d_cam - 需要帮助了解差异
- spring - 登录后的springboot主页
- neo4j - Neo4j,密码。条件合并
- java - 单击 JMenuItem 时如何在 JLabel 上显示图标?
- apache-spark - “java.io.IOException: Failed to delete Spark temp dir”是否会影响您的 spark 结果?
- python - setup.cfg 使用“入口点必须按组列出”声明控制台脚本错误
- java - 为什么 Firebase 在使用 phoneNumb 登录期间不发送身份验证 OTP?如何处理这些异常..?
- python - 如何从 python 脚本调用共享库(.so 文件)?
- java - 超时后停止java CompletableFuture中的线程