IF 与 OR 多个条件进入 FOR 循环

IF 与 OR 多个条件进入 FOR 循环

我对 if 构造中的条件有一个奇怪的问题。下面的代码不应打印“performance_schema”和“innodb”字符串。

/usr/bin/mysql -u $DB_USER -p$DB_PASS -h $DB_HOST -e "SHOW DATABASES" -E | grep -v "*" | gawk '{print $2}' > $DB_NAMES

for DBNAME in `cat $DB_NAMES`; do
  if [[ "$DBNAME"!='performance_schema' ]] || [[ "$DBNAME"!='innodb' ]]; then
    echo "---> $DBNAME selected"
  fi
done

rm -f $DB_NAMES

但输出是:

---> demodb selected
---> information_schema selected
---> innodb selected
---> mysql selected
---> performance_schema selected
---> tmp selected

答案1

一个case声明将使逻辑清晰。

while IFS= read -r dbname; do
    case "$dbname" in
        'performance_schema' | 'innodb') : ;; # do nothing
        *) echo "---> $dbname selected" ;;
    esac
done < "$db_names"

其他注意事项:

相关内容