shell 带参数选择的写法 getopts

2019-03-01  本文已影响4人  SkTj

!/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
;;


!/bin/bash

while getopts "a:b:cdef" opt; do
case opt in a) echo "this is -a the arg is !OPTARG"
;;
b)
echo "this is -b the arg is ! OPTARG" ;; c|d|e|f) echo "this is -c the arg is !OPTARG"
;;
*)
echo "Invalid option: -$OPTARG"
;;
esac
done

上一篇 下一篇

猜你喜欢

热点阅读