e-info128
e-info128

Reputation: 4072

how to process conditional if string is not various strings?

For example:

#!/bin/bash

DATABASES=`ssh root@host "mysql -u root -e 'show databases;'"`;
for database in $(echo $DATABASES | tr ";" "\n")
do
    if [ "$database" -ne "information_schema" ]
    then
        # ssh root@host "mysqldump -u root ..."
        # rsync ...
    fi
done

Need exclude:

How to make 3 conditions in one "if"? in other languages use "or" or "||" but in bash is?

Upvotes: 2

Views: 45

Answers (2)

janos
janos

Reputation: 124646

Inside [[ ... ]] you can use || for OR and && for AND conditions, for example:

if [[ $database != information_schema && $database != sys && $database != Database ]]
then
    # ssh root@host "mysqldump -u root ..."
    # rsync ...
fi

Another alternative is using a case:

case "$database" in
    information_schema|sys|Database) ;;
    *)
        # ssh root@host "mysqldump -u root ..."
        # rsync ...
        ;;
esac

Upvotes: 1

anubhava
anubhava

Reputation: 785058

In BASH you can use @(str1|str2|str3) inside [[...]] to compare against multiple string values:

if [[ $database != @(information_schema|Database|sys) ]]; then
   echo "execute ssh command"
fi

Upvotes: 2

Related Questions