Format translation problem when emailing text file
Hello everyone,
I've been racking my brain on this and cannot seem to find anything on ol' trusty google. I'd appreciate any input. Here is the problem: I have a script that will ssh into several linux servers, run a couple commands and put the information into a file. After its done it mails the file to myself. The script works fine, since the text file looks good. The problem comes when I mail it to myself. When the text comes through in the mail, one of the lines is on top of the line before it. Here's an example of how the input in the file looks: hostname1 CHECK vision.error SIZE: -rw-r--r-- 1 vision vision 689 Apr 29 11:44 vision.error NUMBER OF LINES SHOWING fatal error: 0 =================------------------================== Here's what it looks like when it is emailed to me: hostname1 CHECK vision.error SIZE: -rw-r--r-- 1 vision vision 689 Apr 29 11:44 vision.error NUMBER OF LINES SHOWING fatal error: 0 =================------------------================== In the mailed format, lines 3 and 4 are on top of each other and they should be separated. They are separated in the text file, but for some reason mailing it puts them on top of each other. I know this isn't a huge problem as far as data goes, but it's really bugging me! Anyone run into this sort of problem? Thanks! Keif |
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
And how can we figure out what's wrong with your code if you don't post it? We need to see what generated the mail command, and perhaps some details about the input as well. Also, the text is in a file? Is this a temporary file that was generated by the script? If so, you might try running it through cat -A to see if there are any embedded non-printing characters involved. |
David, thank you for responding. Sorry about the lack of info. Will be sure to get everything you need in future posts.
Here is the code for the script: Code:
#!/bin/sh Code:
lf0002 Code:
lf0002 Let me know if you need any additional information. Thanks again for looking. Keif |
You could try David's suggestion of 'cat -A' to check that line for weird chars (or use od).
A quick fix might be to add an 'echo' in there to enforce a (blank) newline Code:
ssh vision@$STORE "ls -l data/status/vision.error" >> /home/techs/lf-vision.error-large |
Chris, adding the additional echo line seemed to give me a space between the lines so they are not on top of each other:
Code:
lf0002 Keif |
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 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. |
All times are GMT -5. The time now is 01:51 AM. |