1830882 Members
2349 Online
110017 Solutions
New Discussion

Re: unix script

 
SOLVED
Go to solution
so.nimda
Super Advisor

unix script

Hi,

I need some help with a unix script. Basically, I would like the script to email to me a log that it generated. The line that generates the script is :

exec > /test/`basename $0`_`date +%d-%b-%H:%M`.log 2>&1

Is is correct for me to issue the next line in the script as :

mailx -s "Log file" myname@mymail.com <&1

Not sure if this makes sense as I'm a newbie at writing scripts.

Thanks in advance.
8 REPLIES 8
Laurent Menase
Honored Contributor
Solution

Re: unix script

no, you redirect output to a file

LOGFILE="test/`basename $0`_`date +%d-%b-%H:%M`.log"

exec 3>&1 4>&2 >$LOGFILE 2>&1

mailx -s "...." <$LOGFILE
exec 1>&3 2>&4 # to get back the previous output.
marvik
Regular Advisor

Re: unix script

take a look @ man uuencode to attach a fileto your mail.there is some example as well to help you out

Cheers
OFC_EDM
Respected Contributor

Re: unix script

uuencode | mailx -m -s "subject" email@domain.name

The -m is required when attaching a file using mailx on HP-UX.

Example

uuencode /tmp/mylog.log mylog.doc | mailx -m -s "my important log" someone@important.ca

This will send the mylog.log but rename it as mylog.doc. Handy when you receive the email. When you double click the attachment it will open in Word or whichever app is setup to open .doc files.

The Devil is in the detail.
so.nimda
Super Advisor

Re: unix script

Hi Laurent Menase,

Thanks for your reply.

Would you be able to explain the lines :

"exec 3>&1 4>&2 >$LOGFILE 2>&1" & "exec 1>&3 2>&4"

I know that 0 is stdin, 1 is stdout, 2 is stderr but what's 3 & 4? I'm really not good at this - apologies.

Regards

so.nimda
Super Advisor

Re: unix script

Hi marvik & Kevin_Urschatz,

Thanks for your replies.

That's a very elegant way of doing it !

Regards
Dennis Handly
Acclaimed Contributor

Re: unix script

>Would you be able to explain the lines:
"exec 3>&1 4>&2 >$LOGFILE 2>&1" & "exec 1>&3 2>&4"

This is shell trickery.
Basically it dups file 1 to file 3 and file 2 to file 4 (saves them).
Then the second one puts those files back.
Laurent Menase
Honored Contributor

Re: unix script

Yes it is exactly that

exec 3>&1 4>&2 >afile.log 2>anotherfile.log
save fildescriptor 1 to 3 and 2 to 4
before redirecting them to files

and after to put back the filedesc in place
better way
exec 1>&3 2>&4 3<&- 4<&-
3<&- 4<&- close the dupped fildescripor after recovering the initial outputs
so.nimda
Super Advisor

Re: unix script

Hi Dennis & Laurent,

Thanks so much for the help and explanation.

Laurent : I've tried your script and it worked beautifully. Thanks once again !