LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-03-2021, 06:22 AM   #1
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

Rep: Reputation: 35
Question [sSMTP] Right way for newline in body?


Hello,

I'm using the following code to send an email once a week through CRON to check free disk space on a host.

For some reason, the body is either a bit messy, or empty altogether:

Code:
~# cat show.free.disk.space.bash
#!/bin/bash

#This works fine when run manually at the command line
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')

# Body: \Root partition 47 0.000000ull.
printf "To: me@isp.com\nSubject: Free Disk Space\n\n\Root partition $CURRENT% full." | ssmtp me@isp.com

# Empty body :-/
printf "To: me@isp.com\nSubject: Disk Space Alert\nRoot partition $CURRENT% full." | ssmtp me@isp.com
Any idea why?

Thank you.
 
Old 10-03-2021, 06:35 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,596

Rep: Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545

See "man printf" - you need "%%" for a literal percent character.

But removing a percent sign just to re-add it again makes no sense, nor mixing grep/awk/sed like that (any one of them can do it with a single command) - and if you check "man df" you'll see you can simply specify the column you want, then use just sed to delete the headers (no idea why there isn't a df parameter to suppress that.)

Also, instead of embedding a variable you should use printf how it is intended to be used:
Code:
printf 'Root partition %s full.' $(df --output=pcent / | sed 1d)
or
Code:
CURRENT=$(df --output=pcent / | sed 1d)
printf 'Root partition %s full.' "$CURRENT"

In your second example, the body is empty because there is no body - you must separate it from the headers with two lines, as in your first example.


Last edited by boughtonp; 10-03-2021 at 06:48 AM.
 
Old 10-03-2021, 07:31 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
then use just sed to delete the headers (no idea why there isn't a df parameter to suppress that.)
But findmnt has it:
Code:
findmnt -nouse% /
Interestingly enough, findmnt and di (both SysV and BSD-style percentage) give me a slightly different result than df:
Code:
$ df --o=pcent /
Use%
 75%
$ findmnt -nouse% /
 74%
$ di -n -f1 /
  74%
$ di -n -f2 /
  74%
Code:
$ df -h --o=size,used /
 Size  Used
  50G   38G
$ expr 38 \* 2
76
$ df --o=size,used /
1K-blocks     Used
 52399108 38987876
$ bc<<<'scale=1;38987876*100/52399108'
74.4
$ findmnt -osize,used /
SIZE  USED
 50G 37.2G
$ di  -fbBu /
    Size     Size     Used
   50.0G    50.0G    37.2G
$ bc<<<'37.2*2'
74.4

Last edited by shruggy; 10-03-2021 at 07:56 AM.
 
Old 10-03-2021, 09:53 AM   #4
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

Original Poster
Rep: Reputation: 35
Thank you.

Code:
#!/bin/bash

CURRENT=$(df --output=pcent / | sed 1d)

printf 'To: me@isp.com\nSubject: Disk space\n\nRoot partition %s full.'  "$CURRENT" | ssmtp me@isp.com
 
  


Reply

Tags
cron, df



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
match newline, space, newline vincix Programming 13 03-09-2017 03:14 PM
ssmtp sends as email attachment instead of body text rukmeister Linux - Newbie 1 05-06-2013 09:26 AM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 07:57 AM
Hello every body, i'm using redhat linux I don't know about samba server any body seenas Linux - Newbie 2 07-04-2009 03:47 AM
VI replacement: newline for newline not working! jhwilliams Linux - Software 3 08-16-2007 06:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration