Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Redirecting stderr from mailx

SOLVED
Go to solution
Gary Cooper_1
Esteemed Contributor

Redirecting stderr from mailx

Within my script, I want to log/catch any errors that mailx may generate - in particular when it is supplied with an invalid recipient.
when I try to just redirect stderr it just seems to ignore it...

> ll | mailx -s "Test e-mail" bert 2> mail.stderr
> bert... User unknown
<$HOME>/dead.letter... Saved message in <$HOME>/dead.letter
>

Where $HOME is the home directory of the user account that I was logged in as.

Any help would be much appreciated.

Thanks,

Gary
5 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: Redirecting stderr from mailx

Hi Gary:

# ll | mailx -s "Test e-mail" xxx 1>>/tmp/mylog 2>&1

Regards!

...JRF...
Gary Cooper_1
Esteemed Contributor

Re: Redirecting stderr from mailx

Hi James,

It's been a long week! I don't know why I didn't redirect stdout as well in the first place.

As a matter of interest, why didn't my original command work?

Have a good week-end.

Thanks,

Gary
James R. Ferguson
Acclaimed Contributor

Re: Redirecting stderr from mailx

Hi (again) Gary:

> As a matter of interest, why didn't my original command work?

The output messages you really wanted to collect are going to STDOUT not STDERR.

I could have suggested you do:

# # ll | mailx -s "Test e-mail" xxx 1>>/tmp/mylog

...and ignored STDERR entirely.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Redirecting stderr from mailx

>why didn't my original command work?

Because your assumptions and the programmer's about what goes to stderr were different.

With an interactive program like mailx, perhaps this is blurred.

Sometimes as JRF had, you should just log both.
Gary Cooper_1
Esteemed Contributor

Re: Redirecting stderr from mailx

Thanks for the assistance James.