为什么每次我执行脚本时,我都要执行两次才能看到真正的结果,
script_dir=/opt/scripts/x186299
smo_log=/oracle/bxcons21/base/admin/bxcons21/scripts/logs
error_txt=$script_dir/logs/SMO_error.txt
count_error=$script_dir/logs/count_error.txt
yest=$script_dir/logs/yesterday.txt
ECount=`cat $count_error`
rm $error_txt $count_error $yest
echo `TZ=MDT+48 date +%Y%m%d` > $yest
cd $smo_log
#grep -i "Error invoking command" $yest_* >> $error_txt
echo "Error invoking command" >> $error_txt
cat $error_txt | wc -l >> $count_error
if [ $ECount = 0 ];
then
echo "SMO Backup is failed"
exit
else
echo "SMO Backup is succesful"
exit
fi
if I ran this, it will show succesfull, then if I changed this part to
old
"#grep -i "Error invoking command" $yest_* >> $error_txt
"echo "Error invoking command" >> $error_txt
new
"grep -i "Error invoking command" $yest_* >> $error_txt
"#echo "Error invoking command" >> $error_txt
我希望看到正确的失败消息,但不幸的是它显示成功,然后如果我再次执行脚本而不进行更改,它将显示失败消息。
如果只使用 # 就会变得粗体,所以我把 " 放在 grep 和 echo 行上
答案1
你设置得ECount
太早了,此时count_error
脚本第一次执行时文件还是空的:
ECount=`cat $count_error`
...
grep -i "Error invoking command" $yest_* >> $error_txt
...
cat $error_txt | wc -l >> $count_error
if [ $ECount = 0 ];
使用
grep -i "Error invoking command" $yest_* >> $error_txt
...
wc -l $error_txt >> $count_error
ECount=`cat $count_error`
if [ $ECount = 0 ];
相反,或者(如果您以后不需要该文件)
ECount=`wc -l $error_txt`
if [ $ECount = 0 ];