shell 中利用getopts

[root@jboss shell]# cat getopts.sh 

#!/bin/bash

#date=2014-09-16

#is to practice  the getopts

while getopts "a:bc" arg(:前面的變量是一定要跟參數的)

do 

case $arg in

 a)

echo "a's arg:$OPTARG"

;;

 b)

echo "b"

;;

 c)

echo "c"

;;

 ?)

echo "unkown argument"

     exit1

;;

esac

done


[root@jboss shell]# ./getopts.sh -b

b

[root@jboss shell]# ./getopts.sh -c

c

[root@jboss shell]# ./getopts.sh c

[root@jboss shell]# ./getopts.sh -a /root/

a's arg:/root/

如果不加參數的話會報錯的。

[root@jboss shell]# ./getopts.sh -a

./getopts.sh: option requires an argument -- a

unkown argument

./getopts.sh: line 18: exit1: command not found


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章