I tried to write some useful scripts to legally cancel the work more efficiently, and this question suddenly appeared:
Given the very long line $LONGEST_EVER_STRING and a few lines with keywords like $A='foo bar' , $B='omg bbq' and $C='stack overflow'
How should exact keyword matching be used as a condition in a case ?
for word in $LONGEST_EVER_STRING; do case $word in any exact match in $A) do something ;; any exact match in $B) do something ;; any exact match in $C) do something ;; *) do something else;; esac done
I know I can write this way, but it looks very ugly:
for word in $LONGEST_EVER_STRING; do if [[ -n $(echo $A | fgrep -w $word) ]]; then do something; elif [[ -n $(echo $B | fgrep -w $word) ]]; then do something; elif [[ -n $(echo $C | fgrep -w $word) ]]; then do something; else do something else; fi done
Does anyone have an elegant solution? Thank you very much!
source share