Jump to content



Photo

mailx.sh

sh shell script mailx

  • Please log in to reply
4 replies to this topic

#1 Class

Class

    Neowinian

  • Joined: 01-August 05
  • Location: Hungary

Posted 26 June 2014 - 08:58

Hello every neowinian,

 

I'm not a SH expert by any means. I've got a not properly written script at work and it needs major adjustments, because it's not working when there is 1 of the variable attachments missing. It simply wont do anything. It uses the MAILX application on a server. So I was thinking I add some lines to it, like an "if-fi" logical switch that would correct the issue. But here is the current (bad) script:

#!/bin/sh
RCP=$4
ATTACH=$3
SUBJ=$2
RECIPIENT=`echo "${RCP//,/ }"`
ATTACH1=`echo "${ATTACH//+/ /apps/mail-data/}"`
ATTACH2=`echo "${ATTACH1//,/ }"`
SUBJECT=`echo "${SUBJ//,/ }"`
echo $ATTACH2 >/apps/mail-data/log1 
/usr/bin/mailx -s "$SUBJECT" $ATTACH2 $RECIPIENT << __EOF__
This is a generated email from $1 , by any promlems please reply back $5
__EOF__

I was thinking to implement a "-s" as if the file exist and is bigger then 0kb with line like:

 if [-a -s file1 -a -s file2 -a -s file3 ]

But the question is how?

 

The variable is defined into 1 line, the $3 which look like this if it's getting filled:

-a+output1.htm,-a+output2.htm

Would it work if I simply fill it with like???

-a+-s+output1.htm,-a+-s+output2.htm

Thanks in advance for the help! :)




#2 Haggis

Haggis

    Neowinian Senior

  • Tech Issues Solved: 12
  • Joined: 13-June 07
  • Location: Near Stirling, Scotland
  • OS: Debian 7
  • Phone: Samsung Galaxy S3 LTE (i9305)

Posted 26 June 2014 - 12:17

What are you actually trying to do

 

a better description would help :)



#3 OP Class

Class

    Neowinian

  • Joined: 01-August 05
  • Location: Hungary

Posted 26 June 2014 - 13:20

What are you actually trying to do

 

a better description would help :)

 

Yeah, that's the hard thing. :)

 

Atm the above script is not working if any files are missing from the preset attachments, so imagine a situation, where you fill the variable $ATTACHMENT with 2 files and 1 of them is missing (not existing where it should be). MAILX won't send email, because 1 is missing. But I do want him to send it anyway, even if only 1 file is there or not.

 

To continue the text from above, if output1.htm is not there, i still want mailx to send out the output2.htm. Which is only possible if i check in the 1st place which 1 is there and which isn't, and only adding the existing once to the command for mailx, this way it will send the email since only the existing attachment are in the command line.

 

Meanwhile, since I was so desperate I opened a stack overflow question also, but here is what I was trying....

 

So I re-scrabbled the text like this, where I ask the filenames into separate variables, see ATTACH[1-4], then add full file path to it resulting ATTACHM[1-4] and then I check if they exist or not with -s. If certaing file exist I add it into a growing variable called $ATTACHMENTS. Will this work?

#!/bin/sh
RCP=$2
SUBJ=$3
ATTACH1=$4
ATTACHM1=`echo "${/apps/mail-data/$ATTACH1}"`
if [ -s $ATTACHM1 ]
then
ATTACHMENTS=`echo "-a $ATTACHM1"`
else
fi
ATTACH2=$5
ATTACHM2=`echo "${/apps/mail-data/$ATTACH2}"`
if [ -s $ATTACHM2 ]
then
ATTACHMENTS=$(($ATTACHMENTS + `echo "-a $ATTACHM2"`))
else
fi
ATTACH3=$6
ATTACHM3=`echo "${/apps/mail-data/$ATTACH3}"`
if [ -s $ATTACHM3 ]
then
ATTACHMENTS=$(($ATTACHMENTS + `echo "-a $ATTACHM3"`))
else
fi
ATTACH4=$7
ATTACHM4=`echo "${/apps/mail-data/$ATTACH4}"`
if [ -s $ATTACHM4 ]
then
ATTACHMENTS=$(($ATTACHMENTS + `echo "-a $ATTACHM4"`))
else
fi
RECIP=`echo "${RCP//,/ }"`
SUBJECT=`echo "${SUBJ//,/ }"`
echo $ATTACHMENTS >/apps/mail-data/log1 
/usr/bin/mailx -s "$SUBJECT" $ATTACHMENTS $RECIP << __EOF__
This email was generated for $1 , if you have any questions contact: $8
__EOF__

Where you can see, i ask every file individually if they exist and if they do I add them to the $ATTACHMENTS variable, if not they won't get added, hopefully resulting in an output of something like this, if for example file2 and 3 are missing:

mailx -s This is Subject of letter -a output1.htm -a output4.htm some1@somewhere.com Bla-bla

I guess this could be made way more simple, so I'm really open if some1 can help me. This might not evet be working either, because i'm not sure how to/or even adding text to a variable works?!



#4 Haggis

Haggis

    Neowinian Senior

  • Tech Issues Solved: 12
  • Joined: 13-June 07
  • Location: Near Stirling, Scotland
  • OS: Debian 7
  • Phone: Samsung Galaxy S3 LTE (i9305)

Posted 26 June 2014 - 13:31

ok so lets break it down

 

if you do

mailx -s This is Subject of letter some1@somewhere.com Bla-bla

does this work in itself?



#5 OP Class

Class

    Neowinian

  • Joined: 01-August 05
  • Location: Hungary

Posted 26 June 2014 - 18:46

ok so lets break it down
 
if you do

mailx -s This is Subject of letter some1@somewhere.com Bla-bla

does this work in itself?

 

 
Yes. Sure. The app works. Other then that, ofcourse with this I ment a simple demonstration, this look in really different.
 
This is the actual command line that get's executed:

/usr/bin/mailx -s "$SUBJECT" $ATTACHMENTS $RECIPIENT << __EOF__
This is an email. This is the text in it. Bla. Bla. Bla.
__EOF__