Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: find string in multiple other strings

I'm trying to use bash to find if a short string is present in any string "sets". For example,

FRUIT="apple banana kiwi melon"
VEGETABLE="radish lettuce potato"
COLOR="blue red yellow green brown"

MY_CHOICE="kiwi"
MY_CHOICE_GROUP="?"

How can I set MY_CHOICE_GROUP to FRUIT?

I tried to use this StackOverflow solution, but it only works with a single string set.

Originally, I was using arrays to store the options in a set, but given the way bash handles iteration over arrays, it seems a string search would be more efficient.

Many thanks!

like image 555
hao_maike Avatar asked Feb 15 '23 21:02

hao_maike


2 Answers

  1. The simplest way, IMO, would be to just hardcode a bunch of case...esac labels.

    #!/bin/bash
    function lookup()
    {
        case "$1" in
            apple|banana|kiwi|melon)
                echo "FRUIT"
                ;;
    
            radish|lettuce|potato)  
                echo "VEGETABLE"
                ;;
    
            blue|red|yellow|green|brown)   
                echo "COLOR"
                ;;
        esac
    }
    
    MY_CHOICE="kiwi"
    MY_CHOICE_GROUP=$(lookup "$MY_CHOICE")
    
    echo $MY_CHOICE: $MY_CHOICE_GROUP
    

    See it live on ideone

  2. Otherwise, consider associative arrays, see it live on ideone:

    #!/bin/bash
    declare -A groups
    
    groups["apple"]="FRUIT"
    groups["banana"]="FRUIT"
    groups["kiwi"]="FRUIT"
    groups["melon"]="FRUIT"
    
    groups["radish"]="VEGETABLE"
    groups["lettuce"]="VEGETABLE"
    groups["potato"]="VEGETABLE"
    
    groups["blue"]="COLOR"
    groups["red"]="COLOR"
    groups["yellow"]="COLOR"
    groups["green"]="COLOR"
    groups["brown"]="COLOR"
    
    MY_CHOICE="kiwi"
    MY_CHOICE_GROUP=${groups[$MY_CHOICE]}
    
    echo $MY_CHOICE: $MY_CHOICE_GROUP
    
like image 155
sehe Avatar answered Feb 27 '23 18:02

sehe


Only shortening a bit @sehe's answer:

#!/bin/bash

declare -A groups
mkaso() { val="$1"; shift; for key in "$@"; do groups["$key"]="$val"; done; }

mkaso FRUIT apple banana kiwi melon
mkaso VEGETABLE radish lettuce potato
mkaso COLOR blue red yellow green brown
#declare -p groups

MY_CHOICE="kiwi"
MY_CHOICE_GROUP=${groups[$MY_CHOICE]}
echo $MY_CHOICE: $MY_CHOICE_GROUP
like image 43
jm666 Avatar answered Feb 27 '23 18:02

jm666