1.getopts指令
#!/bin/bash
while getopts "a:bc" arg #選項後面的冒号表示該選項需要參數
do
case $arg in
a)
echo "a's arg:$OPTARG" #參數存在$OPTARG中
;;
b)
echo "b"
;;
c)
echo "c"
;;
?) #當有不認識的選項的時候arg為?
echo "unkonw argument"
exit 1
;;
esac
done
[root@zhu ~]# sh a.sh -a nan -b
a's arg:nan
b
[root@zhu ~]# sh zhu.sh -n mingyuexin -l -m -g
the name is mingyuexin
l
m
zhu.sh: illegal option -- g
unknown arg
[root@zhu ~]# cat zhu.sh
#!/bin/bash
while getopts "lmn:" s
do
case $s in
l)
echo "l"
;;
m)
echo "m"
;;
n)
echo "the name is $OPTARG"
;;
?)
echo "unknown arg"
esac
done
getopts optstring name [args]
#getopts 不支援長選項
# optstring :為字元串 如"ab:c" 若是字元後跟冒号,表示該字元選項後需跟參數 ,參數和選項用空格分開。
#當shell執行行,選項後的參數儲存在OPTARG變量中
#當有不是指定的選項時name值傳回?