It is always scary when you get a syntax error on line 11 of a 5 line script.
My first comment would be that you should change:
echo $returned
to:
echo "$returned"
or better yet (removing problems that could be caused depending on the contents of SapLogs.log ):
printf '%s\n' "$returned"
But that alone isn't likely to be your problem. Maybe if you showed us the whole script and told us what OS and shell you're using, we'd have a better chance of figuring out what is going on.
The command uname -a will tell you what version of what OS you're using.
It is very strange that the output from the 1st script you showed us did not contain the line:
=== Checking logs ===
Please add the following line to your script between the 1st and 2nd lines:
set -xv
and show us the output.
I don't see why the following would make any difference in the behavior of your script (except that if there are multiple errors found, they will be printed on separate lines; and a little bit of truth in advertising), but it will use less memory and run faster. Try changing:
returned=`tail -50 SapLogs.log | grep -i "Error"`
echo $returned
if [ -n "${returned}" ]; then
echo "There is no error in the logfile"
fi
to:
tail -50 SapLogs.log | grep -i "Error"
if [ $? ]; then
echo "There is no error in the last 50 lines in the logfile"
fi
My current code is as follows (I've changed filename but content is the same):
#!/bin/bash
set -xv
echo "=== Checking logs ==="
tail -50 SapAdapterInboundRfc-P30-SapAdapterInboundSynchronous.log | grep -i "Error"
if [ $? ]; then
echo "There is no error in the last 50 lines in the logfile"
fi
echo "=== Last log entry ==="
var=$(tail -1 SapAdapterInboundRfc-P30-SapAdapterInboundSynchronous.log | awk '{print substr($0,13,8)}')
echo "Time elapsed since last entry =" $(( $((`date +%s -d $var`)) - $((`date +%s`)) ))
When I delete set -xv the output is:
=== Checking logs ===
checklogs.sh: line 10: syntax error: unexpected end of file
I really don't know why this simply IF doesn't work...
I don't think you can just dump a raw number into [ ] like that without telling it what to do with that number. [ "$?" -ne 0 ] or such.
But if you just want to use the return value, do so directly. The ! reverses it to 'if not':
if ! tail -50 SapAdapterInboundRfc-P30-SapAdapterInboundSynchronous.log | grep -i "Error" >/dev/null
then
echo "There is no error in the last 50 lines in the logfile"
fi
Coronna688,
Ouch. Yes, you're correct. In shell's test , [ "$arg" ] tests for non-empty string; not non-zero and non-empty (as in awk ).
For clarity when reading a script, I don't usually use:
if ! tail -50 SapAdapterInboundRfc-P30-SapAdapterInboundSynchronous.log | grep -i "Error" >/dev/null
when the compound-list is more than about 40 characters long, but that is just my personal preference. We don't want the > /dev/null here because this code is replacing earlier code that was:
and then checking whether "$returned " expands to an empty string.
And, now for the good news...
jedzio,
Know that we know you're using bash on a Linux distro (even though it hasn't been updated for four years), I think I know what the problem is. I should have asked what editor you were using!
To get that error message from bash for the command: set -xv , you must be using an editor that uses the DOS convention of carriage-return followed by newline as the line terminator instead of the UNIX/Linux convention using only the newline.
So, I think you can get your script to work if you write this into a new file (let's call it newscript ):
#!/bin/bash
echo "=== Checking logs ==="
tail -50 SapAdapterInboundRfc-P30-SapAdapterInboundSynchronous.log | grep -i "Error"
if [ $? -ne 0 ]; then
echo "There is no error in the last 50 lines in the logfile"
fi
echo "=== Last log entry ==="
var=$(tail -1 SapAdapterInboundRfc-P30-SapAdapterInboundSynchronous.log | awk '{print substr($0,13,8)}')
echo "Time elapsed since last entry =" $(( $((`date +%s -d $var`)) - $((`date +%s`)) ))
to remove any extraneous carriage return characters in your script and make it executable again.
Let us know what happens.
We usually only see this problem on files that have been copied from Windows systems to UNIX and Linux systems. I strongly suggest that you look at the documentation for your editor to see if there is a way to set the characters used as line terminators. If there isn't any way to set the line terminator to just a newline (AKA linefeed) character, seriously consider learning how to use vi or emacs instead of your current editor. Extraneous carriage returns in shell scripts, source code for other languages, and in data files will cause you no end of grief.
Oh God!
Thank You both for help! Yes, You are right, I am using notepad++, but in the Windows convention instead of Unix... How stupid I am. I've changed the convention to Unix type and my script works as hell!