0

I am trying to write one if condition in Linux.

I have a variable : $check_mrp . I want to check if this variable is not equal to either of the following three values : WAIT_FOR_LOG or APPLYING_LOG or WAIT_FOR_GAP then echo message : "Please start the Manged Recovery Process".

For example if variable value is equal to any of the three (WAIT_FOR_LOG or APPLYING_LOG or WAIT_FOR_GAP) then it should not echo the message since services are running.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
RahulM
  • 29
  • 7
  • 2
    Have you searched for "bash conditional if expressions" or anything like that? What have you tried? – lurker Nov 05 '15 at 12:13

3 Answers3

2

Use a regular expression as described in check if string match a regex in BASH Shell script:

[[ $name =~ ^(WAIT_FOR_LOG|APPLYING_LOG|WAIT_FOR_GAP)$ ]]

Note the ^ and $ to indicate that it must be exactly this string.

Test

$ name="bla"                                                             
$ [[ $name =~ ^(WAIT_FOR_LOG|APPLYING_LOG|WAIT_FOR_GAP)$ ]] && echo "yes"

$ name="WAIT_FOR_LOG"
$ [[ $name =~ ^(WAIT_FOR_LOG|APPLYING_LOG|WAIT_FOR_GAP)$ ]] && echo "yes"
yes
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
1
if [[ ! $check_mrp =~ "WAIT_FOR_LOG|APPLYING_LOG|WAIT_FOR_GAP" ]]; then echo "Please start the Manged Recovery Process"; fi
Joe Young
  • 5,749
  • 3
  • 28
  • 27
0
if [ x$check_mrp == x"WAIT_FOR_LOG" ] || [ x$check_mrp == x"APPLYING_LOG" ] || [ x$check_mrp == x"WAIT_FOR_GAP" ]
then
    exit 0
fi

echo "Please start the Manged Recovery Process"
exit 1
Yaron
  • 1,199
  • 1
  • 15
  • 35