0

I'm trying to write output to a file, the correct output is printed in the successful file and the same is printed in the unsuccessful log. In the unsuccessful log it should print only unsuccessful logs but it is printing successful logs also.

for TASKARN  in `aws ecs list-tasks --cluster APPS --desired-status RUNNING --region us-east-1 --service-name coreservice-service | jq .taskArns[] | sed 's/.$//; s/^.//' | cut -d":" -f 6 | cut -d"/" -f 3`
do
echo $TASKARN
cd /tmp/
val=`find core-$TASKARN.log -maxdepth 0 -daystart -mtime -1`
if [ $val == core-$TASKARN.log ]
then
echo " core-$TASKARN.log is present " >> /opt/successfull.log
else
echo " core-$TASKARN.log is not present " >> /opt/unsuccessfull.log
fi
done

In successful log

core-26f374a6a.log is present

In unsuccessful log

core-26f374a6a.log is not present

Mani
  • 85
  • 10
  • 3
    shellcheck your code, fix the issues and then post the updated version if you still have a problem. See https://www.shellcheck.net/ – Ed Morton May 05 '19 at 03:35
  • Agree with Ed. Shellcheck produced 5 findings on your code. Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww May 05 '19 at 03:52
  • 1
    Thanks. It worked. It is a great help for sharing the shellcheck.net tool. – Mani May 05 '19 at 03:54

1 Answers1

0

Apart from the shellcheck suggestions, you might consider the below points too..

  • Replace TASKARN with taskarn. Full-uppercase variables are reserved as environment variables. You may accidentally override an environment variable if you set a full uppercase variable.
  • Consider removing cd /tmp/. Instead do :

    val=$(find /tmp/ "core-${taskarn}.log" -maxdepth 0 -daystart -mtime -1)
    # When you embed a variable inside string consider curly braces. See reference.
    

Reference

sjsam
  • 21,411
  • 5
  • 55
  • 102