我想使用 getopts 将一些参数解析为 bash 脚本,但希望能够访问选项列表中未包含的其余参数。例如,如果我有一个电话:
% script -a -b param -c param -d other arguments here
Run Code Online (Sandbox Code Playgroud)
我会:
while getopts "ab:c:d" opt ; do
.
done
Run Code Online (Sandbox Code Playgroud)
获取“此处的其他参数”的最简单方法是什么,getopts 不应处理这些参数?
小智 18
解析 arg 时需要移动,或放入
完成解析后移动 $((OPTIND -1)) ,然后以通常的方式处理,例如
while getopts "ab:c:d" opt ; do
.
done
shift $(expr $OPTIND - 1 )
while test $# -gt 0; do
echo $1
shift
done
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14781 次 |
| 最近记录: |