RahulM
RahulM

Reputation: 29

if condition in Linux

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.

Upvotes: 0

Views: 460

Answers (3)

fedorqui
fedorqui

Reputation: 289495

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

Upvotes: 2

Joe Young
Joe Young

Reputation: 5875

if [[ ! $check_mrp =~ "WAIT_FOR_LOG|APPLYING_LOG|WAIT_FOR_GAP" ]]; then echo "Please start the Manged Recovery Process"; fi

Upvotes: 1

Yaron
Yaron

Reputation: 1242

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

Upvotes: 0

Related Questions