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 use the following to send an email at the end of a script.

echo "Script finished on `date`" | /usr/bin/Mail -s "Script complete" "myaccount@myserver.com".

However, I want to echo the same message onto the screen as well. How do I do that in the same statement?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

The easiest way is probably to tee the message to stderr as well as stdout:

echo "Script finished on date" | tee /dev/stderr \
    | /usr/bin/Mail -s "Script complete" "myaccount@myserver.com"

tee duplicates its input to multiple destinations, including stdout. By default, both stderr and stdout go to the screen; you're redirecting stdout to Mail, leaving just stderr going to the screen.

If you need it in stdout for some reason, you could redirect it back using a subshell (or several other ways):

(
    echo "Script finished on date" | tee /dev/stderr \
        | /usr/bin/Mail -s "Script complete" "myaccount@myserver.com"
) 2>&1
share|improve this answer
3  
alternatively, tee to a file and then mail the file...very useful if the output is huge and you want to send it, e.g., as a base64-encoded gzipped MIME attachment. or just redirect all output to a file (e.g. with exec &> $LOGFILE) and then restore stdout, cat the file to stdout and mail it. –  cas Sep 19 '12 at 22:48
    
Awesome derobert. Thank you very much. –  sammy Sep 20 '12 at 16:45
    
Thank you Craig. Since my text in this case is very small, I will go with the response from derobert. –  sammy Sep 20 '12 at 16:45
    
Hmm.. this did work however I realized that I needed to add more to my problem description. I used this solution as the last line of my script. I used a "nohup" command on my script to collect all the output of my script as "nohup.out" and all I see is just this one line "Script finished on xxxxx". Prior to using this solution, I got all the script output fine. Is there a way how I can append the output of this last one line instead of erasing it all? –  sammy Sep 24 '12 at 15:06
1  
Using "tee -a" solved the problem. –  sammy Sep 24 '12 at 16:09

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.