Skip to content
Advertisement

Multiple strings to one string comparison bash

I have a shell script like below. It works based on a condition that if $table contains test then executes small.sh, elde big.sh.

if [[ "$table" =~ "test" ]]
then 
  echo "events"
else
  echo "history"
fi

Now I want to check if tables contains test , _test_and_results and success in if [[ "$table" =~ "test" ]].

How can I do that

I have tried like below

if [[ "$table" =~ "test" && "$table" =~ "success" ]];
then
    echo "events"
else
    echo "history"
fi

But when I pass table name abc1_success It is printing history instead of events.

What am I doing wrong here

Advertisement

Answer

If I understand your question correctly, then you should use ‘or’ instead of ‘and’. Ie || instead of &&.

Your current script is true only if both test and success are found.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement