3

All, thanks for your help... This should be easy:

My script prints this to stdout/terminal perfectly... its pretty in the script but not below... ???? :

# Print to stdout
echo "------- CAPACITY TEST FAILED -------"
echo -n "  SYSTEM NAME: " ; uname -n
echo -n "  USER DETAIL: " ; whoami
echo "  PARTITION:   $PART "
echo "  USED SPACE:  $USEDSPC "
echo "  THRESHOLD LIMIT OF $THRESH% EXCEEDED"
echo "------------------------------------"

I have tried numerous mailx options/formats but coming up a few fry's short of a 'happy meal'... Any help is awesome.

Please note I cannot change the red hat server in any way, shape, or form... frankly they don't want me breathing on it... my job is to script around it!1

Evan Carroll
  • 30,763
  • 48
  • 183
  • 315
SSDdude
  • 171
  • 2
  • 12
  • First post sorry: I want to output this multiline stdout message into a mailx body and have it formatted nicely – SSDdude Feb 23 '18 at 23:19
  • #stdout format ------- CAPACITY TEST FAILED ------- SYSTEM NAME: cdpra05a0400 USER DETAIL: u626844 PARTITION: /dev/sda1 USED SPACE: 22 THRESHOLD LIMIT OF 20% EXCEEDED

    only prettier

    – SSDdude Feb 23 '18 at 23:30

2 Answers2

4

If you pipe some stdout into mailx's stdin, it will send that as the contents of the email

echo "test body" | mailx -s test_subject username@example.com

If your script just runs and only outputs that output, you should be able to get your desired result with the following:

/path/to/script | mailx -s subject_here username@example.com

If this bit of bash is just part of a bigger script, you can get all of this to be sent over email by combining all of your above lines into one big fat echo:

echo -e "------- CAPACITY TEST FAILED -------\n  SYSTEM NAME: $(uname -n)\n  USER DETAIL: $(whoami)\n  PARTITION:   $PART \n  USED SPACE:  $USEDSPC \n  THRESHOLD LIMIT OF $THRESH% EXCEEDED\n------------------------------------" | mailx -s subject_here username@example.com
ImRomeo
  • 41
  • 1
  • 2
    +1. you can also use a heredoc instead of all those echo statements. e.g. cat <<EOF | mailx -s test_subject username@example.com followed by the text (and shell variables), followed by EOF on a line by itself. Also, quoted strings can be multi-line so you don't need echo -e and \n inside the double-quotes. – cas Feb 24 '18 at 01:35
1

Inside your script, you can also create a sub-shell by adding "(" ")" and pipe its output:

(echo "------- CAPACITY TEST FAILED -------"
 echo -n "  SYSTEM NAME: " ; uname -n
 echo -n "  USER DETAIL: " ; whoami
 echo "  PARTITION:   $PART "
 echo "  USED SPACE:  $USEDSPC "
 echo "  THRESHOLD LIMIT OF $THRESH% EXCEEDED"
 echo "------------------------------------" ) | mailx ...
JJoao
  • 12,170
  • 1
  • 23
  • 45