一个完善的shell脚本应该能处理行如 -option 的命令行选项,bash 有一个内置的命令 getopts 可用来处理命令行选项
示例,用法 getopts.h -x -y YARG -z ZARG *
bbkf:/yzyfb/report/cdq/bash$cat getopts.sh
#!/bin/bash
# getopts.sh - Using getopts
##############################
while getopts xy:z: opt;
do
case $opt in
x) xopt='-x set';;
y) yopt="-y set and called with $OPTARG" ;;
z) zopt="-z set and called with $OPTARG" ;;
\?) echo 'USAGE: getopts.sh [-x] [-y arg] [-z arg] file ...'
exit 1
esac
done
shift $(($OPTIND - 1 ))
echo ${xopt:-'did not use -x'}
echo ${yopt:-'did not use -y'}
echo ${zopt:-'did not use -z'}
echo "Remaining command-line arguments are:"
for f in "$@"
do
echo "\t$f"
done
阅读(1602) | 评论(0) | 转发(0) |