I'm having an issue with tail & grep in shell script if statement. If I run tail -5 mylog.log | grep -c "Transferred: 0" in shell, it runs as it should, but in this shell script if statement:
# Parse log for results
if [ tail -1 "$LOGFILE" | grep -c "Failed" ] ; then
RESULT=$(tail -1 "$LOGFILE")
elif [ tail -5 "$LOGFILE" | grep -c "Transferred: 0" ] ; then
RESULT=""
else
RESULT=$(tail -5 "$LOGFILE")
fi
I get
... [: missing `]'
grep: ]: No such file or directory
for both of the grep lines.
It's clearly to do with the closing ] being seen as part of the grep (and thus missing) but I'm using the correct whitespace so I can't figure out what's going on? What am I doing wrong here?
Thanks, PJ
Immediate Issue / Immediate Fix
Take out the brackets.
[is not part ofifsyntax. Rather, it's a synonym for the command namedtest(which is typically both available as a shell builtin and an external binary, like/bin/testor/usr/bin/test).Thus, your original code was running
[ tail -1 "$logfile", and piping its result togrep -q "Failed" ]. The first[was failing because it didn't see an ending]-- which is mandatory when invoked by that name rather than with the nametest-- and also because its parameters weren't a test it knew how to parse; and the secondgrepdidn't know what the]it was being piped meant, trying to find a file by that name.Other Notes
Try to run external commands -- like
tail-- as little as possible. There's a very significant startup cost.Consider the following, which runs
tailonly once: