Operating System - HP-UX
1834134 Members
1979 Online
110064 Solutions
New Discussion

error mailing file in script

 
Ratzie
Super Advisor

error mailing file in script

I have a #!/bin/sh script that performs a while loop and writes output to a file.

After the while loop I have an if statement that checks if -s then mails file.

Problem. If I run the script it does not send the email, the file exists but does not send the file.BUT, if I dont remove the file, and run it again, it emails it. Its like it does not see the file the first time.

Do I have to close the file, after the while / done

If so, how?
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: error mailing file in script

Post the script please.

How you are writing this is important.

Your first write to the file should be

output for example cat > filename

Subsequent output >> filename

The second statement appends.

Every time your script finishes appending, the file is closed.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ratzie
Super Advisor

Re: error mailing file in script

while read POS CON NAME DATE TIME UPTIME
do
if [ "$UPTIME" -gt 1 ];then
#make a list of person, date and time resource removed
echo $NAME,$DATE,$TIME >> $PUNTED
exec $CLNUP "/pos:"$POS "/con:"$CON"\r"
fi
done < $FORTH_DOWN
ll $PUNTED

#if resources removed send email to admins
if [ -s $PUNTED ];then
echo "found someone to punt"
$MAIL -s "$SUBJECT" $MAILTO < $PUNTED
# $MAIL -s "$SUBJECT" $MAILTOCELL <
fi
Hazem Mahmoud_3
Respected Contributor

Re: error mailing file in script

I have a perl script that I use to email reports that works great. Includes checks for file attachments that are 0 size.

#Module used to send the email
use MIME::Lite;

#Initialize variables
my $from = 'reports@dcu_opc_gw2.dupagecu.com';
my $reply = 'hmahmoud@dcu_opc_gw2.dupagecu.com';
my $subject = 'Automated Report';

$testing = system("test -s $location");
if ($newfile =~ /rstagtb4.txt/) {
$newfile =~ s/.txt/.doc/;
}
if ($testing == 0) {
if ($newfile =~ /stmtprts.txt/) {
$mime_msg = MIME::Lite->new( #setup email message
From => $from,
To => $to_address,
CC => $reply,
"Reply-To"=> $reply,
Subject => "$newfile - $subject",
Data => $message1
)
or die "Error creating MIME body\n";
$mime_msg -> attach( #setup file attachment
Path => $location,
Filename=> $newfile
)
or die "Error attaching file\n";
$mime_msg -> send or die "Error sending report\n"; #send email
}
else {
$mime_msg = MIME::Lite->new( #setup email message
From => $from,
To => $to_address,
CC => $reply,
"Reply-To"=> $reply,
Subject => "$newfile - $subject",
Data => $message
)
or die "Error creating MIME body\n";
$mime_msg -> attach( #setup file attachment
Path => $location,
Filename=> $newfile
)
or die "Error attaching file\n";
$mime_msg -> send or die "Error sending report\n"; #send email
}

print "Report: $newfile, sent to $to_address\n";


-Hazem
Dave La Mar
Honored Contributor

Re: error mailing file in script

Find attached a number of ways of using mailx.
We use this extensively in our shop.

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
Michael Schulte zur Sur
Honored Contributor

Re: error mailing file in script

Hi,

this I found:
MAIL If this variable is set to the name of a mail file and
the MAILPATH variable is not set, then the shell
informs the user of arrival of mail in the specified
file.

what have you got in $MAIL?

greetings,

Michael
Michael Schulte zur Sur
Honored Contributor

Re: error mailing file in script

Hi again,

can you post the output of the script?

have fun,

Michael
Ratzie
Super Advisor

Re: error mailing file in script

I have MAIL set to /usr/bin/mailx

I do not get any output. The script runs with no errors. There is data in the file to send.

If I put an ls right after the done of the while loop it does not find the file. Only after the script exits.
Michael Schulte zur Sur
Honored Contributor

Re: error mailing file in script

Hi,

please use another variable than $MAIL to avoid confusion with system variables. My suggestion: $MAILBIN

greetings,

Michael
Siem Korteweg
Advisor

Re: error mailing file in script

I think you are mixing stdin/stdout of the loop and the program CLNUP. Why are you using exec to run this program? This terminates the current process.

Use the following code:

while read a b c
do
if [ "${c}" -gt 1 ]
then
echo "${a}" >>output_file
${CLNUP} ${a} ${b} fi
done
if [ -s output_file ]
then
mailx -s Subject ${MAILTO} fi
Rita C Workman
Honored Contributor

Re: error mailing file in script

Greetins,

First...mailx is a messaging/broadcast utility. And according to some in the threads below..doesn't support sending attachments. Might I suggest try using something like elm (a mail processing system).

Here are a couple threads on sending attached files. There are a few ways to do this..some like 'uuencode' or 'mpack'. My personal favorite is the handy 'include' statement file, because of it's flexibility in sending any number of types of files...and getting them there the way you want.

Here is even an old quickie script I wrote once for doing this..hope you find it useful !
==================
#!/bin/sh
#
#
# This little script runs a couple
# Performance Programs for FileNet
# and then setups and emails them
# to user
# Written 11/30/00 by Rita Workman
###########################################
#
# Setup my only real variable
##############################
loc=/fnsw/local/tmp/perf
#
##############################
# Now cleanup/move any old files
# to ~/oldfiles for storage
# And run their Perf Programs
###############################
cat /dev/null /home/rworkman/dummy/jmailer
cat /dev/null /home/rworkman/dummy/tmp
mv $loc/*.Z $loc/oldfiles
cd /fnsw/local/ps
/fnsw/local/ps/perfreports.hpu
cd /fnsw/lib/perf
/fnsw/lib/perf/getreports
compress $loc/*
#
################################
# Setup Include file for email
################################
for mail in $loc/*
do
print "[include $mail application/msword base64]" >> /home/rworkman/dummy/tmp 0>&1
done
#
#
#
# Email it !!
################################
elm -s "Daily Performance Files from Rita $(date)" user@dot.com < /home/rworkman/dummy/jmailer
#
echo "Email to user done $(date)"
#
# all done !!
===========================

http://forums1.itrc.hp.com/service/forums/parseCurl.do?CURL=%2Fcm%2FQuestionAnswer%2F0%2C%2C0x41950559ff7cd4118fef0090279cd0f9%2C00.html&admit=716493758+1072703120567+28353475

http://forums1.itrc.hp.com/service/forums/parseCurl.do?CURL=%2Fcm%2FQuestionAnswer%2F1%2C%2C0xd6a2afe90f1cd71190050090279cd0f9%2C00.html&admit=716493758+1072703480470+28353475

Hope this helps,
Rgrds,
Rita