如何在此 if 条件中包含“>”? bash脚本

如何在此 if 条件中包含“>”? bash脚本
 date=$(date +'%d-%m-%y')
  mkdir -p $date
  echo "Enter DB User";
  read DBUSER ;
  echo "Enter Password" ;
  read PASS ;
  echo "Enter DB Name" ;
  read DBNAME ;
  echo "Enter Dump file name with .sql"
  read DESTDUMP


if [ mysqldump -u$DBUSER -p$PASS $DBNAME > $DESTDUMP ] ; then 

mv $DESTDUMP.sql $date

rsync -RravhP $date $DESTINATION

else 

echo "DATABASE DUMP WENT WRONG. CHECK YOUR ENTRIES"

fi

在“if”语句中它给出了一个错误,因此该脚本转到“else”部分。它给了我“if”语句的输出

 + [ mysqldump -uuser1 -ppass1 db1 ]
 script: 21: [: mysqldump: unexpected operator
 + echo DATABASE DUMP WENT WRONG. CHECK YOUR ENTRIES
 DATABASE DUMP WENT WRONG. CHECK YOUR ENTRIES

答案1

方括号([ ... ][[ .. ]])用于特定测试(请参阅man test)。如果您只想查看命令是否已成功完成(即给出退出代码0),请不要使用它们。简单地:

if command; then
   do_something
else
   do_something_else
fi

所以,就你而言:

if mysqldump -u$DBUSER -p$PASS $DBNAME > $DESTDUMP; then 
    mv $DESTDUMP.sql $date
    rsync -RravhP $date $DESTINATION
else 
    echo "DATABASE DUMP WENT WRONG. CHECK YOUR ENTRIES"
fi

相关内容