getopts 更多选项触发一个事件

Getopts more options trigger one event

我目前正在更新我编写的第一个打印房间时间表的脚本 我想使用 getopts 来解析我的选项,现在我的问题是:

我的选项是打印所有房间的时间表:

到目前为止一切顺利。

不,我只想使用以下选项打印一个房间:

我如何使用 getopts 管理它?类似的东西?

while getopts :a:b:c:d:hP: ARG; do
  case $ARG in
    ab)  #set option "a"
      echo "-a used: $OPTARG"
      echo "OPT_A = $OPT_A"
      ;;
    cd)  #set option "b"
      echo "-b used: $OPTARG"
      echo "OPT_B = $OPT_B"
      ;;

这可能吗?我希望你明白我的意思...

您可以简单地定义一个变量 ONE_ROOM,如果给出选项 r,该变量将设置为 true。您无需手动区分参数的所有可能组合。

#!/bin/bash

ONE_ROOM="0"

while getopts rwecO: ARG; do
  case $ARG in
    r) ONE_ROOM="1";;
    w) ;;
    e) ;;
    c) ;;
    O) ;;
  esac
done

if [ "$ONE_ROOM" -eq "1" ] ; then
  # print only one room
else
  # print all rooms
fi