1

Why every time I execute my script, I tooks 2 execution before I see the true result,

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

I expect to see failed message right, but unfortunately it showing succesfull then if I execute again the script, without changing, it will show the failed message.

getting bold if only used #,so I put " on grep & echo line

  • You don't have a variable called yest_, so $yest_* would expand to * and then the * would expand to all filenames in the current directory. I'm not quite sure I know what you meant to do here, possibly "${yest}_"*. In general, your script lacks quoting for every variable expansion. Please see shellcheck.net – Kusalananda Jun 12 '18 at 6:52
1

You set ECount too early, at that moment the count_error file is still empty when the script executes the first time:

ECount=`cat $count_error`
...
grep -i "Error invoking command" $yest_* >> $error_txt
...
cat $error_txt | wc -l >> $count_error
if [ $ECount = 0 ];

Use

grep -i "Error invoking command" $yest_* >> $error_txt
...
wc -l $error_txt >> $count_error
ECount=`cat $count_error`
if [ $ECount = 0 ];

instead, or (if you don't need the file later on)

ECount=`wc -l $error_txt`
if [ $ECount = 0 ];
  • awesome, it works. Thanks a lot my friend.. – Jayskiee08 Jun 13 '18 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.