7

Possible Duplicate:
Capturing multiple line output to a bash variable

I have what is probably a basic scripting question, but I haven't been able to find an answer anywhere that I've looked.

I have an awk script that processes a file, and spits out a list of disabled systems. When I call it manually from the command line, I get formatted output back:

$awk -f awkscript datafile

The following notifications are currently disabled:

host: bar
host: foo

I am writing a wrapper script to call from my crontab, which will run the awk script, determine if there is any output, and email me if there is. It looks like (simplified):

BODY=`awk -f awkscript datafile`
if [ -n "$BODY" ]
then
echo $BODY | mailx -s "Disabled notifications" me@mail.address
else
echo "Nothing is disabled"
fi

When run this way, and confirmed by adding an echo $BODY into the script, the output is stripped of the formatting (newlines are mainly what I'm concerned with), so I get output that looks like:

The following notitifications are currently disabled: host: bar host: foo

I'm trying to figure out how to preserve the formatting that is present if I run the command manually.

Things I've tried so far:

echo -e `cat datafile | awkscript` > /tmp/tmpfile
echo -e /tmp/tmpfile

I tried this because on my system (Solaris 5.10), using echo without the -e ignores standard escape sequences like \n . Didn't work. I checked the tmpfile, and it doesn't have any formatting in it, so the problem is happening when storing the output, not when printing it out.

BODY="$(awk -f awkscript datafile)"
echo -e "$BODY"

I tried this because everything I could find, including some other questions here on stackoverflow said that the problem was that the shell would replace whitespace codes with spaces if it wasn't quoted. Didn't work.

I've tried using printf instead of echo, using $(command) instead of `command`, and using a tempfile instead of a variable to store the output, but nothing seems to retain the formatting.

What am I missing, or is there another way to do this which avoids this problem all together?

Community
  • 1
  • 1
Madasi
  • 73
  • 1
  • 1
  • 6
  • Isn't using an output file an option? – fge Jan 12 '12 at 15:02
  • You mean have the awk script generate an output file, instead of printing output to stdout? I was just investigating what would be involved in that. I know I can redirect the print actions in awk to a file, I just have to check that they append instead of clobber. – Madasi Jan 12 '12 at 15:06
  • You can append to a file by using `>>` from the shell instead of `>`. `>` indeed clobbers, but `>>` appends. – fge Jan 12 '12 at 15:08
  • And escape sequences are `\n` – Kevin Jan 12 '12 at 15:22
  • I did not, thank you for the catches, I'll edit the question to be correct. – Madasi Jan 12 '12 at 15:25
  • Possibly a duplicate, I didn't find that question when I was searching, but the accepted answer for that question doesn't work for me. – Madasi Jan 12 '12 at 15:59

2 Answers2

8
BODY=`awk -f awkscript datafile`
if [ -n "$BODY" ]
then echo "$BODY" | mailx -s "Disabled notifications" me@mail.address
else echo "Nothing is disabled"
fi

Note the double quotes in the echo.

You can simplify this version, too:

echo -e `cat datafile | awkscript` > /tmp/tmpfile
echo -e /tmp/tmpfile

to just:

tmpfile=/tmp/tmpfile.$$
awkscript > $tmpfile
if [ -s $tmpfile ]
then mailx -s "Disabled notifications" me@mail.address < $tmpfile
else echo "Nothing is disabled"
fi

Backquotes are useful (but better written as $(cmd args)) but do not have to be used everywhere.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • The first example does not work, as the formatting loss occurs when storing the value into $BODY. I tested this by using `echo -e "$BODY" at one point. However, the second and third examples work perfectly. I made a mistake when testing the redirect to a temp file (my debug code printed the value of the wrong variable), so I missed that this was working. This is the fix I will probably implement. – Madasi Jan 12 '12 at 15:52
  • Don't use `echo -e`; use `echo`! – Jonathan Leffler Jan 12 '12 at 16:12
  • I tried that as well, same result. I was using -e by default because on my system, using echo without the -e causes it to ignore escape sequences like \n and print them instead. Probably an artifact of running on the version of Solaris I'm on. However, to test I included the lines `echo $BODY` and `echo -e $BODY` to see the value, and got the same non-formatted result from both. – Madasi Jan 12 '12 at 16:30
  • 1
    @Madasi: the first example works for me (with a different command, since I don't have your awkscript). Testing with either `echo $BODY` or `echo -e $BODY` is useless; you *absolutely must* use double-quotes around `"$BODY"` or it looses the formatting before echoing it. – Gordon Davisson Jan 12 '12 at 17:46
  • You are correct. I would have sworn that I tested with the double quotes in place and that it didn't work. However, since I didn't keep the test versions of the script, I went back and tried it again, and it works correctly. Therefore, the evidence clearly says that I screwed up my testing and was fighting myself all along. Thanks! Sorry I was so thick headed about insisting that I'd tried it with double quotes all along. – Madasi Jan 12 '12 at 19:04
3

Using quotes should work, and does for me:

$ cat test.sh
#!/bin/bash -e

BODY=`cat test.in`
if [ -n "$BODY" ]; then
        echo "$BODY" | mailx -s "test" username
else 
        echo "Nothing"
fi

$ cat test.in

The following notifications are currently disabled:

host: bar
host: foo

$ ./test.sh
$

And this sends me a properly formatted email.

Kevin
  • 53,822
  • 15
  • 101
  • 132
  • I tested this on my system, and you are correct, this does give a properly formatted email. – Madasi Jan 12 '12 at 15:34
  • It seems that an intermediate file, whether it be a temp file for the output or the formatted test.in here does hold formatting when catted into a variable's value, but formatted output on stdout does not hold formatting when placed directly into a variable. – Madasi Jan 12 '12 at 15:55