bash getopts多个参数或默认值
发布时间:2020-12-16 01:50:08 所属栏目:安全 来源:网络整理
导读:所以我有一个关于在bash中获取opts的问题.我想获取参数的值,如果它们存在但是如果它们不存在则使用默认值.因此脚本应该采用目录和整数,但如果未指定,则$PWD和3应为默认值.这是什么 while getopts "hd:l:" opt; do case $opt in d ) directory=$OPTARG;; l )
所以我有一个关于在bash中获取opts的问题.我想获取参数的值,如果它们存在但是如果它们不存在则使用默认值.因此脚本应该采用目录和整数,但如果未指定,则$PWD和3应为默认值.这是什么
while getopts "hd:l:" opt; do case $opt in d ) directory=$OPTARG;; l ) depth=$OPTARG;; h ) usage exit 0;; ? ) usage exit 1;; esac 解决方法
您可以在while循环之前提供默认值:
directory=mydir depth=123 while getopts "hd:l:" opt; do case $opt in d ) directory=$OPTARG;; l ) depth=$OPTARG;; h ) usage exit 0;; *) usage exit 1;; esac done echo "<$directory> <$depth>" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读