如果字符串不是各种字符串,如何处理条件?

how to process conditional if string is not various strings?

例如:

#!/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

需要排除:

如何将3个条件合而为一"if"?在其他语言中使用 "or" 或“||”但在 bash 是?

在 BASH 中,您可以在 [[...]] 中使用 @(str1|str2|str3) 来比较多个字符串值:

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

[[ ... ]] 中,您可以使用 || 作为 OR,使用 && 作为 AND 条件,例如:

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

另一种选择是使用 case:

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