Shell script, output of a command to a variable and saving formatting

Possible duplicate:
Capturing multiple line output to a bash variable

I have a question, which is probably the main scenario, but I could not find the answer anywhere I looked.

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

$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 that will run the awk script, determine if there is any output, and send me an email if there is one. It looks (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 you run this path and are confirmed by adding echo $BODY to the script, the output loses formatting (this mainly concerns line feeds), so I get an output that looks like this:

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

I am trying to figure out how to keep 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 an echo without -e ignores standard escape sequences such as \ n. Does not work. I checked the tmp file and there is no formatting in it, so the problem arises when saving the output, and not when printing it.

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

I tried this because everything I could find, including some other questions here on stackoverflow, said the problem was that the shell would replace whitespace codes with spaces if it weren’t cited. Does not work.

I tried using printf instead of echo, using $ (command) instead of `command` and using a temporary file instead of a variable to store the output, but nothing of the kind preserves the formatting.

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

+6
source share
2 answers
 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 echo .

You can also simplify this version:

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

simply:

 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 should not be used everywhere.

+8
source

Using quotes should work for me too:

 $ 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 that sends me a correctly formatted email.

+3
source

Source: https://habr.com/ru/post/905834/


All Articles