[[email protected] 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
[[email protected] shell]# ./getopts.sh -b
b
[[email protected] shell]# ./getopts.sh -c
c
[[email protected] shell]# ./getopts.sh c
[[email protected] shell]# ./getopts.sh -a /root/
a‘s arg:/root/
如果不加参数的话会报错的。
[[email protected] shell]# ./getopts.sh -a
./getopts.sh: option requires an argument -- a
unkown argument
./getopts.sh: line 18: exit1: command not found
时间: 2024-11-14 19:01:09