0

我希望选项 -h 可以单独使用,而不与任何其他选项(例如 -h -l)一起使用。我该怎么做?

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

case $2 in
-l)bb;;
esac

case $3 in
-p)cc;;
esac

谢谢。

4

1 回答 1

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!
于 2012-10-07T06:56:45.213 回答