bash getopts多个参数或默认值

格雷格

所以我有一个关于在bash中获得选择的问题。我想获取参数的值(如果存在),但是如果不存在,则使用默认值。因此,脚本应该使用目录和整数,但是如果未指定它们,则$ 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>"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章