2

Currently, I have some flag -x, along with some others:

while getopts abcx opt; do
  case $opt in
    a) a=true; ;;
    b) b=true; ;;
    c) c=true; ;;
    x) x=true; ;;
  esac
done

However, I want to make it so that adding an x will increase the level of the flag. I've seen this with v for verbose with extra v's increasing the verbosity. How do I detect this in my Bash script?

mbomb007
  • 3,788
  • 3
  • 39
  • 68
  • https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash – MatBailie Dec 08 '20 at 22:25
  • @MatBailie Though that question and its answers cover a lot, there is little explanation along with all the code. It's hard for someone who doesn't know much Bash. – mbomb007 Dec 09 '20 at 15:25

1 Answers1

6

I want to make it so that adding an x will increase the level of the flag.

Use a counter:

unset a b c x
while getopts abcx opt; do
  case $opt in
    a) a=true; ;;
    b) b=true; ;;
    c) c=true; ;;
    x) ((x++)); ;;
  esac
done
echo "x=$x"

Then use it as:

bash optscript.sh -abcxxx
x=3
anubhava
  • 761,203
  • 64
  • 569
  • 643