2016-05-17 183 views
0

錯誤隱藏輸出需要從腳本隱藏錯誤從標準輸出

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 

我只需要符合數據庫中不存在

+0

提示:該消息可能來自您的'db_status = $(...)'行,而不是來自您的'elif [...]'行...... – twalberg

回答

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)