Operating System - HP-UX
1833611 Members
3307 Online
110062 Solutions
New Discussion

shell script to mail data file not including data

 
Joe Robinson_2
Super Advisor

shell script to mail data file not including data

I'm using the following shell to grab a data file and email it to another office. For some reason I can't explain, it recently stopped including the data file and just sends a blank email. I'd be grateful for a tip what the problem on this might be...

dest=username@email address
if [ -f /home/robinson/myfile* ]
then
cd /home/robinson
for file in `ls myfile*`
do
/usr/contrib/bin/gzip -c $file > ${file}.gz
echo "[include ${file}.gz application/octet-stream base64]" > ${file}.gz.uu

elm -s "Subject - $file" $dest < ${file}.gz.uu

rm -f ${file}.gz
rm -f ${file}.gz.uu

exit

8 REPLIES 8
RAC_1
Honored Contributor

Re: shell script to mail data file not including data

ll /home/robinson/myfile*

Do you have that file?? and more than zero bytes??
There is no substitute to HARDWORK
Biswajit Tripathy
Honored Contributor

Re: shell script to mail data file not including data

One obvious mistake is, there is no "done" for
the "for" loop. The syntax for "for" loop is:

for file in ....
do
....
done

You don't get any syntax error because of "exit".

- Biswajit
:-)
harry d brown jr
Honored Contributor

Re: shell script to mail data file not including data


The best way to send files is with uuencode:

for i in `echo filename1 filename2 filename3 ...` do uuencode $i $i.txt
done|mailx -m -s "test" username@whereever.com

live free or die
harry d brown jr
Live Free or Die
A. Clay Stephenson
Acclaimed Contributor

Re: shell script to mail data file not including data

I suspect that you have a timing problem. The file is being removed before elm can process it. It would be a bit more apparent if you included siome text in your mail before the [include] clause.
If it ain't broke, I can fix that.
Joe Robinson_2
Super Advisor

Re: shell script to mail data file not including data

Let's try this again...didn't include all the particulars last time. The data file is there, with data in it.

#dest=username@emailaddress

if [ -f /home/robinson/file* ]
then
cd /home/robinson
for file in `ls file*`
do
/usr/contrib/bin/gzip -c $file > ${file}.gz
echo "[include ${file}.gz application/octet-stream base64]" > ${file}.gz.uu
elm -s "Subject - $file" $dest < ${file}.gz.uu

rm -f ${file}.gz
rm -f ${file}.gz.uu
mv /home/robinson/file1 /bkup
exit

done
fi
john korterman
Honored Contributor

Re: shell script to mail data file not including data

Hi,
try running your script like this:
# sh -x and post the output.

Btw: do not use "for file" as "file" is also a command.

regards,
John K.
it would be nice if you always got a second chance
Biswajit Tripathy
Honored Contributor

Re: shell script to mail data file not including data

I would change the script to this:

cd /home/robinson
for file in `ls file*`
do
if [ -s $file ]
then
/usr/contrib/bin/gzip -c $file > ${file}.gz
echo "[include ${file}.gz application/octet-stream base64]" > ${file}.gz.uu
elm -s "Subject - $file" $dest < ${file}.gz.uu
sleep 3
rm -f ${file}.gz
rm -f ${file}.gz.uu
mv /home/robinson/file1 /bkup
exit
fi
done

The "sleep 3" should take care of Clay
Stephenson's suggestion that it might be timing
issue. You could remove it later if that turns out
to be a non-issue.

I'm not sure [ -f /home/robinson/file* ] is a good
idea as -f expects a single file name.

- Biswajit
:-)
A. Clay Stephenson
Acclaimed Contributor

Re: shell script to mail data file not including data

I'll stick with my original answer. I assumed the exit statement was a typo for done.

I think the included file is being unlinked before elm can read it. Remember, the include is a directive to read an external file. It's possible that elm has simply queued the mail job at this point but not processed the include statement.

Consider this ananlogous situation:
lp -dmyprinter myfile
rm myfile

It's possible that myfile will never be printed because though the printjob has been scheduled; it hasnt actually starter reading the file. On the other hand:
lp -dmyprinter -c myfile
OR
cat myfile | lp -dmyprinter
rm myfile

will work everytime because the file is either copied or is piped to the lp command before it can be used.

Again, I can't tell from your blank email but it would be a good idea to incluse some text before and after your [include] clause so that it would be very obvious what is happening.
If it ain't broke, I can fix that.