Search code examples
bashswitch-statementlookupglob

Bash Script looking for string in parameter to set a variable


I'm quite new to linux, not so much in programing language. I'm looking for a way to set a variable (var2) to a given value depending on matching the last 3 Bytes of a MAc Adress in var1

Here's an example :

var1="AA:BB:CC:DD:EE:FF"

here is my "lookup table" base on the 3 last values of the mac adress"

DD:EE:FF > var2="Home"
AB:CD:EF > var2="Garage"
FE:DC:GH > var2="Bedroom1"
... (Max 20)

Important thing : The "lookup table" needs to be "readable" because it needs to be easily configurable. I would have used very ugly IF statements otherwise. :-)

Thanks to all the people who will take the time to help me on this.


Solution

  • Looks like all you need is a case statement.

    case $var1 in
     *:DD:EE:FF) var2="Home";;
     *:AB:CD:EF) var2="Garage";;
     *:FE:DC:GH) var2="Bedroom1";;
    esac