没有累积选项的壳牌案例

时间:2012-10-07 06:50:59

标签: shell

我希望选项-h可以单独使用而不会累积任何其他选项,例如(-h -l)。我该怎么办?

case $1 in
-h)help;;
-f)aa;;
esac

case $2 in
-l)bb;;
esac

case $3 in
-p)cc;;
esac

感谢。

1 个答案:

答案 0 :(得分:1)

示例代码

  #!/bin/bash                                                                                                                                           

    while getopts "ha" opt; do
      case $opt in
        a)
          echo "-a was triggered!" >&2
          ;;
        h)
          echo "-h was triggered!" >&2
          ;;
      esac
    done

测试用例

 ./example.bash -a -h
-a was triggered!
-h was triggered!