zhouandtao 发表于 2018-8-29 07:26:08

shell处理命令行选项getopts

#!/bin/bash  

  
echo $*
  
while getopts ":a:bc" opt    #第一个冒号表示忽略错误
  
do
  
      case $opt in
  
                a ) echo $OPTARG
  
                  echo $OPTIND;;
  
                b ) echo "b $OPTIND";;
  
                c ) echo "c $OPTIND";;
  
                ? ) echo "error"
  
                  exit 1;;
  
      esac
  
done
  
echo $OPTIND
  
shift $(($OPTIND - 1))
  
#通过shift $(($OPTIND - 1))的处理,$*中就只保留了除去选项内容的参数,可以在其后进行正常的shell编程处理了。
  
echo $0
  
echo $*


页: [1]
查看完整版本: shell处理命令行选项getopts