【发布时间】:2015-07-16 23:49:24
【问题描述】:
我在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字符串中选项标志后面的:对getopts表示“需要参数”。 (字符串开头的:完全是另外一回事。)
标签: linux bash scripting command-line-arguments getopts