繁体   English   中英

bash getopts验证选项

[英]bash getopts validate options

我在bash脚本中有以下代码:

# argument validation and usage help
usage()
{
cat << EOF
usage: $0 options

File Integrity Monitoring Script:

OPTIONS:
   -b      input file for [backup]
   -r      input file for [diff report]
   -l      list backup files
EOF
}

if [ $# -eq 0 ]
  then
    usage
    exit 0
fi


while getopts ":b:r:l:" o; do
    case "${o}" in
        b)
            B=${OPTARG}
            backup $B
            ;;
        r)
            R=${OPTARG}
            diffcheck $R
            ;;
        l)
            ls -ld /root/backup/* | awk -F/ '{print $(NF)}'
            ;;
        *)
            usage
            exit 0
            ;;
    esac
done
shift $((OPTIND-1))

题:

如果使用选项-b它需要inputfile ,但-l它只是需要打印的目录列表不经过任何的说法,有没有简单的方法来找出哪个选项需要argument

spatel # ./final.sh -l
usage: ./final.sh options

File Integrity Monitoring Script:

OPTIONS:
   -b      input file for [backup]
   -r      input file for [diff report]
   -l      list backup files

如果我通过任何参数都可以

spatel # ./final.sh -l xxx
May-06-15-10-03
May-06-15-10-04
May-06-15-10-19
May-06-15-11-30

getopts参数中选项之后的:表示它接受参数。 由于-l不需要选项,因此您应该使用

getopts getopts ":b:r:l"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM