我在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发布于 2015-05-06 17:28:44
参数中指向getopts的选项后面的getopts指示它接受一个参数。由于-l不需要选项,所以应该使用
getopts getopts ":b:r:l"https://stackoverflow.com/questions/30083560
复制相似问题