Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm using the following very basic shell script I copied off the net to list the contents of my database backup folder and mail them regularily:

MYSQLLIST=$(ls -lhG /var/backups/mysql/daily/)
SUBJECT="daily backup finished"
EMAIL="logging@localhost"
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "dailybackup.sh ran successfully" > $EMAILMESSAGE
echo "MYSQL:" >> $EMAILMESSAGE
echo $MYSQLLIST >> $EMAILMESSAGE
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

However - regardless the mail reader - in the mail I receive there are no linebreaks between the rows of the ls output, which makes the message terribly hard to read. How could I produce linebreaks identical to those I get when executing ls -lhG on the shell?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Try dropping the command sutstitution that stuffs a variable and then echos it back and just redirect the output of ls straight to the message file.

SUBJECT="daily backup finished"
EMAIL="logging@localhost"
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "dailybackup.sh ran successfully" > $EMAILMESSAGE
echo "MYSQL:" >> $EMAILMESSAGE
ls -lhG /var/backups/mysql/daily/ >> $EMAILMESSAGE
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

Edit: You could further clean up this script with exactly the same result like this:

(
    echo "dailybackup.sh ran successfully"
    echo "MYSQL:"
    ls -lhG /var/backups/mysql/daily/
) | mail -s "Daily backup finished" "logging@localhost"

This runs a few commands in a subshell and then directs the entire output from the subshell straight into mail without the fuss of writing a temporary file.

share|improve this answer
    
This did the trick an simplified the script. Thanks! –  Morris Aug 25 '11 at 8:02
    
@Morris: The script needs more work than that if simplification is the goal! See my edit. –  Caleb Aug 25 '11 at 8:11
    
Thank you, the additions are much appreciated. –  Morris Aug 25 '11 at 15:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.