lipeng 发表于 2018-8-23 09:47:12

shell 中利用getopts

  # cat getopts.sh
  #!/bin/bash
  #date=2014-09-16
  #is to practicethe 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
  # ./getopts.sh -b
  b
  # ./getopts.sh -c
  c
  # ./getopts.sh c
  # ./getopts.sh -a /root/
  a's arg:/root/
  如果不加参数的话会报错的。
  # ./getopts.sh -a
  ./getopts.sh: option requires an argument -- a
  unkown argument
  ./getopts.sh: line 18: exit1: command not found

页: [1]
查看完整版本: shell 中利用getopts