0

I have a case statement, when calling on the script I need it to accept these numbers as possible options of -d. There is a lot of them, so how do you do this without writing 100 more lines of code. I see many programs which allow an option to be any number so how do you do that?

case "$1:$2" in
    -d:0.1|-D:0.1)
        shift
        echo "0.1"
        shift
        ;;
    -d:0.2|-D:0.2)
        shift
        echo "0.2"
        shift
        ;;
    -d:0.3|-D:0.3)
        shift
        echo "0.3"
        shift
        ;;
    -d:0.4|-D:0.4)
        shift
        echo "0.4"
        shift
        ;;
     ...
#It continues this pattern all the way to 5.0, increasing by 0.1 each time ^^

esac

Example of Usage

$ example.sh -d 0.1
> 0.1

Is there a faster way to do this without writing all that code?

  • Uhm so `case "$1" in -d|-D) echo "$2"; shift 2;; esac` ? – that other guy Jul 11 '18 at 20:18
  • 1
    Why don't you just `echo $2` since it's the same thing? – Barmar Jul 11 '18 at 20:18
  • Are you asking how to tell whether `$2` is a number between `0.1` and `5.0`? You could use a regular expression. – Barmar Jul 11 '18 at 20:20
  • This code is shrinked, I'm wondering if you could have the case functionality without writing tons of lines of code when I'm pretty sure it could be avoided. ``$1``` is ```-d``` or ```-D```, ```$2``` is the number. This question may sound unclear but i really don't know any other way to describe it. – Caucasian Malaysian Jul 11 '18 at 20:42

0 Answers0