Thanks for posting the code. It would help a bit if you could format it better though, with proper indentations of sub-blocks and line spacing between logical sections.
Scripting With Style
Both of the outputs you posted show the same text, BTW. Is this an error on your part, or is it really that way?
But before we address this directly, I want to comment on the script itself.
First of all, does this have to be a POSIX-compatible
/bin/sh script? If you can use
bash or
ksh instead, and their more advanced features, some things would be easier to code for.
The first, main error I noticed was the
improper use of a for loop, along with a
Useless Use Of Grep.
(Or actually, in this case, the
awk command can be replaced with
read, so it's really the unnecessary command here.)
$(..) is highly recommended over `..` too.
Finally, it's not a good idea hard-code data structures like filenames into a script. Set them first as variables at the top, or import them from elsewhere.
The script as I would write it:
Code:
#!/bin/sh
infile='/etc/storelist'
outfile='/home/techs/lf-vision.error-large'
mailadd=myemail@hostname.com
while read -r store _; do
server="vision@$store"
echo "CHECK vision.error SIZE"
echo "$store"
ssh "$server" "ls -l data/status/vision.error"
echo "NUMBER OF LINES SHOWING fatal error:"
ssh "$server" "grep -ic 'with Fatal ERROR=' data/status/vision.error"
echo =================------------------================== >>
done <<INPUT >>"$outfile"
$( grep '^lf0' "$infile" )
INPUT
mail -s "Check for Fatal Error Message in LF" "$mailadd" < "$outfile"
exit 0
In order to code it for
/bin/sh compatibility I used a
here document for the
while loop input. With bash we could use a
process substitution instead. "The
_" in the
read command is a throwaway variable that takes the unwanted part of the line, so we get only the first field in the
$store variable.
Notice also how you can redirect the entire output of the loop at once to the outfile. It would be even better not to use an external file at all, though, if you don't need it for other things. The script could easily be modified to store the output in a variable or array for later use. Or it could set it up as a function and it's output redirected directly into
mail.
I notice that there's nothing removing or clearing the outfile after it's been used. Subsequent runs will include all the old data as well, unless you add a line to delete it.
And one final suggestion. If possible, how about combining the two
ssh commands into one? There's no need to make two connections when one will do, although that would probably require modifying the output format.
Now to get back to the main problem. If we assume that its the main outfile that contains the improper lines, then the only possible explanation I can see within the script is that the
ssh'd "
ls -l" command isn't tacking on a trailing newline for some reason. But since you've already tried adding another
echo, that doesn't seem to be the case.
So if that isn't the source of the error, and the outfile does contain the correctly formatted lines, then the problem must have something to do with the way
mail processes the input. But that's a program that I don't have much experience with.