[英]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.