隐藏stdout错误的输出

时间:2016-05-17 15:39:33

标签: bash

需要隐藏脚本中的错误

for s in $s_list; do
    if [ "${s}" = "test" ]; then
        db_status=$(mysql -h localhost -P 3306 -u test -ptest -e "show create database test;"  | awk {'print $1'} | tail -n 1 )
        db_status_error=$(mysql -h localhost -P 3306 -u test -ptest -e "show create database test;" 2>&1 | awk {'print $1'} | tail -n 1 )
        # echo $db_status_error   
        if [ "$db_status" == "test" ]; then  
            echo "Database exist, need wait..." 
            sleep 2;
        elif [ "$db_status_error" == "ERROR" ] < /dev/null > /dev/null 2>&1 ; then
            echo "Database does not exist" 
            sleep 2;
            exit 0
        fi
    fi
done

结果是

ERROR 1049 (42000) at line 1: Unknown database 'test'
Database does not exist

我需要与数据库不存在的行

1 个答案:

答案 0 :(得分:1)

设置stderr后,您需要将/dev/null重定向到db_status

db_status=$(mysql -h localhost -P 3306 -u test -ptest -e "show create database test;" 2> /dev/null | awk {'print $1'} | tail -n 1 )