0

im currently updating my first script i wrote to print room schedules I want to use getopts to parse my options now my question:

My Options are to print the schdule for all rooms are:

  • -w for the next day

  • -e to print the schedule for monday on friday

  • -c current day

  • -O with an given offset

So far so good.

No i want to print only one room with following options:

  • -rw
  • -re
  • -rc
  • -rO

How can i manage that with getopts? Something like that?

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"
      ;;

Is that possible? I hope you understand what i mean...

Nico
  • 323
  • 4
  • 14

1 Answers1

0

You can simply define a variable ONE_ROOM which is set to true if the option r is given. You don't need to manually distinguish between all possible combinations of your arguments.

#!/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
Eduard Itrich
  • 836
  • 1
  • 8
  • 20
  • I will check that. Thanks. – Nico Aug 01 '17 at 12:04
  • Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted. – Eduard Itrich Aug 22 '17 at 03:32
  • I haven't marked it because i was to busy with stuff that i couldn't even try it. Easy Fix wonder why i wasn't able to think of it by myself. Works perfectly – Nico Sep 01 '17 at 13:25