lundi 19 novembre 2018

Better way then having multiple elif statement in bash

I have a file called file.txt which has some random numbers from 1 to 100. So a script reads the file and executes bunch of commands and print some statements.

One way of achieving the result could be something like these but having 100 if and elif statement in a script doesn't look so nice.

for i in `cat file.txt`; do
  echo "Displaying" $i
    if [[ $i = 1 ]]; then
        echo "blah blah blah for" $i
        command1
        command2
    elif [[ $i = 2 ]]; then
        echo "blah blah blah for" $i
        command3
        command4
        command5
    elif [[ $i = 3 ]]; then
        echo "blah blah blah for" $i
        command6
        command7
    elif [[ $i = 4 ]]; then
        echo "blah blah blah for" $i
        command8
        command9
        command10
    elif [[ $i = 5 ]]; then
        echo "blah blah blah for" $i
        command11
        command12
        command13
        command14
    elif [[ $i = 6 ]]; then
        echo "blah blah blah for" $i
        command15

        ....
        ....
        ....
        ....
        ....
        ....
    elif [[ $i = 99 ]]; then
        echo "blah blah blah for" $i
        command310
        command311
        command312
        command313
        command314
    elif [[ $i = 100 ]]; then
        echo "blah blah blah for" $i
        command315
    fi
done

Is there any better or smarter way to do these on bash

Thanks

Aucun commentaire:

Enregistrer un commentaire