bash - Bash getopt 接受多个参数
问题描述
我正在编写一个使用 getopt 来解析参数的脚本。到目前为止我的解决方案只接受一个参数。有没有办法让这个解决方案接受多个参数(例如'-f 和 -l)?
链接中的解决方案对我不起作用。 Bash getopt 接受多个参数
代码: '''
while getopts "f:l:" option; do
case "${option}" in
f) firstdate=${OPTARG}
shift
;;
l) lastdate=${OPTORG}
;;
*)
echo "UsageInfo"
exit 1
;;
esac
shift
done
'''
解决方案
首先,您有一个错字:OPTORG
应该是OPTARG
.
更重要的是,您不需要调用shift
. getopts
负责消费和跳过每个选项和参数。
while getopts "f:l:" option; do
case "${option}" in
f) firstdate=${OPTARG} ;;
l) lastdate=${OPTARG} ;;
*)
echo "UsageInfo"
exit 1
;;
esac
done
推荐阅读
- drupal - 在 Drupal 8 中显示注册导航项(匿名用户)和隐藏(经过身份验证的用户)
- inno-setup - 如何为自己的目的重用/修改许可证对话框
- ios - 在 RealmSwift 中更新对象?
- java - IntelliJ IDEA 方法名称缩进
- amazon-web-services - AWS 文件上传
- citrus-framework - 所有测试用例的全局随机数
- react-native - React Native 调试器 - 在启动后不久执行的代码中没有命中断点
- r - R 和 SQL Server AzureVM 之间的数据传输速度降低
- javascript - 带有 Promises 的 Node JS cron 脚本
- c++ - 如何在运行时找出我的 c++ 应用程序中可用的视频编解码器?