0

Below code is working fine:

#!/bin/bash
str='fail'
var1='pass'
var2='ok'
var3='fail'
var4='pass'

case $str in
   $var1|$var2|$var3|$var4)
   echo yes
   ;;
   *) echo no
   ;;
esac

When I execute this, as expected I get output yes.

In above code, value of variables are not hard-coded, these are coming from previous run, so it keep changing. Here the problem is, sometime it comes like:

var3='partial|fail'

Any variable value can change like this. So in this case it gives no. What should I do change in my code so it handle this situation and match fail word and show the result yes?

3
  • Maybe the answer would be to change the code that generates your variables' content. What does it look like? Commented Apr 13, 2017 at 14:44
  • @lgeorget: Unfortunately that can not be changed. Whatever changes I have to do is only this code. Commented Apr 13, 2017 at 14:53
  • If var3 has the value partial|fail, does this allow the two values partial and fail for str, or does it allow the one value partial|fail? Commented Apr 14, 2017 at 21:19

3 Answers 3

0

Do the case on str,

case $var1$var2$var3$var4 in
   *$str*) 
          echo yes 
   ;;
   *) 
          echo no 
   ;;
esac
4
  • This might generate false positives, viz., if some part of $str lies in one variable and other part lies in the next, then it would generate a yes whereas, technically speaking it shouldn't. Commented Apr 13, 2017 at 16:05
  • No with the data proposed by @serenesat Commented Apr 13, 2017 at 16:07
  • The data can be anything, not even the OP has a control over that. By that logic, if the data is fixed, we might just as well echo a yes in that scenario. Commented Apr 13, 2017 at 16:10
  • You can put an abbutal and then the method is general, in this case the "|" char that appears in the context of the problem is a good candidate, then the code looks, case $var1"|"$var2"|"$var3"|"$var4 in *$str*) echo yes; ;; *) echo no; ;; esac Commented Apr 13, 2017 at 16:19
0
       case $var1 in *"$str"* ) echo yes ;;
   * ) case $var2 in *"$str"* ) echo yes ;;
   * ) case $var3 in *"$str"* ) echo yes ;;
   * ) case $var4 in *"$str"* ) echo yes ;;
   * ) echo no ;; esac;; esac;; esac;; esac

With your approach we will have to resort to eval.

Another method:

unset -v f
A=( "$var1" "$var2" "$var3" "$var4" )
for var in "${A[@]}"; do case $var in *"$str"* ) f=; echo yes; break;; esac; done
${f+:} echo no
2
  • You can propose any better approach also if you have. I am not bounded with this only. :) Commented Apr 13, 2017 at 15:45
  • @serenesat As desired, another method has been proposed. You may want to test that. Commented Apr 13, 2017 at 16:03
0
#!/bin/bash
str="fail";
echo ${str} |awk -v var1="pass" \
-v var2="ok" \
-v var3="partial|fail" \
-v var4="pass" \
'{ if (( $0 ~ var1 ) || ( $0 ~ var2 ) || ( $0 ~ var3 ) || ( $0 ~ var4 )) {
  print "yes";
} else {
  print "no";
}}'

You can replace quoted var assignments above with on-the-fly values as needed. If I've understood requirement, it is that "yes" be output whenever "str" is matched to some part of one of the vars?

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.