0

I am using getopts to pass parameters to a shell script.

For example, I want to make operations on two sections in the script, that both start with the same letter. I also dont want to choose any other letter. For example I have two projects : project 1 and project2 and both of them start with the letter p.

Is there any way, that I can provide full strings such as --project1 etc. Or is there any other solution?

while getopts "project1 project2" opt;
# This ofcourse would not work. So, whats the solution?

Is there any alternative to getopts?

yinnonsanders
  • 1,831
  • 11
  • 28
infoclogged
  • 3,641
  • 5
  • 32
  • 53
  • Possible duplicate of [Using getopts in bash shell script to get long and short command line options](https://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options) – yinnonsanders Jul 21 '17 at 14:41

1 Answers1

0

You can use an options argument. It should look something like

while getopts p: flag
do
    case $flag in
        p ) OARG=$OPTARG;;
    esac
done

echo $OARG

getopts stores the option passed in in OPTARG.

You would invoke the script using

./script.sh -p "project1"
yinnonsanders
  • 1,831
  • 11
  • 28
  • project1 and project2 are not mutually exclusive. sometimes, it might be required to run them both at the same time. Maybe my example project1 and project2 was not correct. – infoclogged Jul 21 '17 at 13:33
  • In that case you would probably want to use `getopt` (no s) – yinnonsanders Jul 21 '17 at 14:40