0

I am confused how to read in multiple options using getopts and how to use one directory as the source for both options. Let me be more specific...

If I were to say:

./rpsm.sh -u -g /some/directory

or

./rpsm.sh -ug /some/directory

How would I use /some/directory as the directory for both options -u and -g?


Also, how would I list the results of -u then list the results of -g (or vice versa) after running the script?

  • The second part of your question has nothing to do with `getopts`, and is entirely dependent on how you implement `./rpsm.sh`. – chepner Apr 25 '17 at 18:05
  • For each parameter if it is not given but the other is, have it default to the other parameter. You'd need to implement this behavior yourself though. – Simon Hibbs Apr 26 '17 at 10:26

1 Answers1

0

I would define a third option (-B for both?) to be used to set both -u and -g simultaneously.

while getopts "u:g:B:" opt; do
    case $opt in
      u) u_dir=$OPTARG ;;
      g) g_dir=$OPTARG ;;
      B) u_dir=$OPTARG
         g_dir=$OPTARG
         ;;
    esac
done

getopts doesn't provide for the type of shortcut you want.

chepner
  • 497,756
  • 71
  • 530
  • 681