处理选项_查找选项_分离参数和选项
#!/bin/bash
# 需要同时使用选项和参数。标准做法是使用特殊字符(--)将两者分开。
# extract command-line options and parameters
# 当脚本遇到双连字符时,变回停止处理选项,将剩下的部分作为命令行参数。
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "found the -a option";;
-b) echo "found the -b option";;
-c) echo "found the -c option";;
--) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
echo
count=1
for param in $@
do
echo "parameter #$count: $param"
count=$[ $count + 1 ]
done
echo
exit
# ./extractoptionsparams.sh -a -b -c test1 test2 test3
# ./extractoptionsparams.sh -a -b -c -- test1 test2 test3
./extractoptionsparams.sh -a -b -c test1 test2 test3
./extractoptionsparams.sh -a -b -c – test1 test2 test3