bash - 用于参数检查的 getopts bash 查询
问题描述
我有一个如下的 getops 脚本..我遇到了这个脚本的问题..用法是
Usage: ./getoptstest.sh [-f] [ -i INSTANCES ]
如果我通过了,./getoptstest.sh -f -i 1.2.3.4 ,结果效果很好,
Result
f good
i good
1.2.3.4
No args
但是如果我切换选项, ./getoptstest.sh -i -f ,它会将“-f”作为“-i”的参数。
Result:
i good
-f -->This is the result of echo statement printing the argument for -i.
No args
我希望脚本严格检查 -i 参数,而不是将 -f 作为其参数。-f 本身就是一个不同的选项。请帮我修改什么来实现这一点。
这是我的脚本..
while getopts ":hfi:" opt; do
case $opt in
i) INSTANCES="$OPTARG"
echo "i good"
echo $INSTANCES
;;
f) FORCE=1
echo "f good"
;;
h) usage
exit;;
\?) echo "Usage: Invalid option: -$OPTARG" >&2
;;
:) echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
*) usage
exit;;
esac
done
解决方案
推荐阅读
- python - 带有“if”子句的列表理解如何工作?
- php - 如何修复“在 null 上调用成员函数 update()”错误?
- go - Go 惯用的类型转换函数命名
- html - HTML 敏捷包获取类
- reactjs - 如何在 react js 中创建单独的管理路由?
- django - 如果我将应用程序重新组织到应用程序文件夹,Django 无法识别应用程序的 __init__.py 中指定的包
- php - 用 DOM 获得的路径指向 php 中的播放器
- python - 我们如何将某个变量的先前值存储在python中的某个其他变量中?
- javascript - 从嵌套的 json 中获取所有键值
- r - 如何在 csv 文件中忽略带引号的换行符以创建 Hive 外部表?