Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Mail sent from variable is not aligned in shell script

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 28 May 2017, 4:24 AM EDT
Location: San Jose, CA, USA
Posts: 10,308
Thanks: 521
Thanked 3,587 Times in 3,055 Posts
Quote:
Originally Posted by viay View Post
Hello Don,

As you suggest I tried the new script,

a.)all of your assignments like this are wasting CPU and memory by using cat when it is not needed. Try:
Yes


Code:
export maillist=abc@gmail.com;
#df -PH /d04 /d05 /u01 /export | grep -vE '^Filesystem|none|cdrom'|awk '{ print $5 " " $6 }' | while read output;
df -PH | grep -vE '^Filesystem|none|cdrom|swdepot'|awk '{ print $5 " " $6 }' > $HOME/monitor/diskcheck.log;

if [ -s "$HOME/monitor/log/disk_alert.log" ]; then
#Getting variables and compare with old
  usep=($(awk '{ print $1 }' $HOME/monitor/diskcheck.log | cut -d'%' -f1));
  partitions=($(awk '{ print $2 }' $HOME/monitor/diskcheck.log | cut -d'%' -f1));
  usep1=($(awk '{ print $1 }' $HOME/monitor/log/disk_alert.log | cut -d'%' -f1));
else
   cat "$HOME/monitor/diskcheck.log" > "$HOME/monitor/log/disk_alert.log";
  usep=($(awk '{ print $1 }' $HOME/monitor/diskcheck.log | cut -d'%' -f1));
  partitions=($(awk '{ print $2 }' $HOME/monitor/diskcheck.log | cut -d'%' -f1));
  usep1=($(awk '{ print $1 }' $HOME/monitor/log/disk_alert.log | cut -d'%' -f1));
fi

echo ${usep[@]};
echo ${usep1[@]};
unset alertlist
# index = i
for i in ${!usep[@]}; do
        if [ ${usep[$i]} -gt 80 ]; then
                if [ ${usep[$i]} -gt ${usep1[$i]} ]; then
                        # Temp to store current percentage diff and partition to store in a variable.
                        tmp="$(echo -e "Running out of space \"${partitions[$i]} (${usep[$i]}%)\" on $(hostname) as on $(date)")";
                        echo "$tmp"
                        #tmp=$(printf "%s\n" "(${usep[$i]}%) percent used on ${partitions[$i]}");
                        alertlist=("${alertlist[@]}" "$tmp")
                fi
        fi
done

if [ ${#alertlist} -gt 0 ]; then
        printf '%s\n' "${alertlist[@]}" | mail -s "ALERT:  Running out of space" $maillist;
        echo "Following text sent as body of mail to $maillist:"
        printf '%s\n' "${alertlist[@]}"
fi                     #alertlist holds the values to send mail.

While I run the script it seems everything okay as usual the output of run script is,

Code:
[vijay@localhost monitor]$ sh disk_alert.sh
19 1 39 3 40 93 1 14 96
19 1 39 3 40 83 1 14 66
Running out of space "/u05 (93%)" on localhost.local as on Tue May 16 
Running out of space "/u02 (96%)" on localhost.local as on Tue May 16 
Following text sent as body of mail to abc@gmail.com:
Running out of space "/u05 (93%)" on localhost.local as on Tue May 16 
Running out of space "/u02 (96%)" on localhost.local as on Tue May 16

When I receive the mail it's remains with the old format

Code:
Running out of space "/u05 (93%)" on localhost.local as on Tue May 16 Running out of space "/u02 (96%)" on localhost.local as on Tue May 16

Yes. But we need more information.

With this latest version of your script we know that the text being sent is correctly formatted and that your recipient's mail reader is reformatting the text in the message before displaying it.

What application is being used by abc@gmail.com to read mail?

If the mail reader being used is expecting HTML input, the thread Sendmail ignoring line endings
might provide the information we need to work around your problem by changing the line:

Code:
        printf '%s\n' "${alertlist[@]}" | mail -s "ALERT:  Running out of space" $maillist;

to something like:

Code:
        { echo '<HTML><BODY>'
          printf '%s<br />\n' "${alertlist[@]}"
          echo '</HTML></BODY>'
        } | mail -s "ALERT:  Running out of space" $maillist;


Last edited by Don Cragun; 1 Week Ago at 04:19 PM..
Sponsored Links
    #9  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 28 May 2017, 4:24 AM EDT
Location: San Jose, CA, USA
Posts: 10,308
Thanks: 521
Thanked 3,587 Times in 3,055 Posts
And for the part of your previous post asking about filesystem mount order...
Quote:
Originally Posted by viay View Post
Hello Don,

... ... ...
1.)
Note, however, that if a mounted filesystem is ever unmounted or a new filesystem is mounted between runs of your script, there is no guarantee that the order of the percentage values stored in the arrays usep and usep1 are in the same order!

Is their any solution for this?
Use associative arrays instead of indexed arrays for the percentages (using the filesystem mount point as the subscript). I don't have access to a 4.x version of bash so, this is totally untested, but it should come close to what you need (including the previously suggested HTML mail body changes) and run faster (since all of the invocations of awk and cut are gone):

Code:
export maillist=abc@gmail.com;
#df -PH /d04 /d05 /u01 /export | grep -vE '^Filesystem|none|cdrom'|awk '{ print $5 " " $6 }' | while read output;
df -PH | grep -vE '^Filesystem|none|cdrom|swdepot'|awk '{ print $5 " " $6 }' > $HOME/monitor/diskcheck.log;

unset usep usep1
declare -A usep usep1

if [ -s "$HOME/monitor/log/disk_alert.log" ]; then
  #Getting variables and compare with old
  while read -r pct fs
  do	pct=${pct%\%}
	usep["$fs"]=$pct
	printf '%s(%s) ' "$fs" "$pct"
  done < "$HOME/monitor/diskcheck.log"
  echo
  while read -r pct fs
  do	pct=${pct%\%}
	usep1["$fs"]=$pct
	printf '%s(%s) ' "$fs" "$pct"
  done < "$HOME/monitor/log/disk_alert.log"
  echo
else
  cp "$HOME/monitor/diskcheck.log" "$HOME/monitor/log/disk_alert.log"
  # Note that if disckcheck.log and disk_alert.log are identical, usep[$i] can
  #   never be greater than usep1[$i], so we might as well quit now.
  exit
fi

unset alertlist
for fs in ${!usep[@]}; do
        if [ ${usep["$fs"]} -gt 80 ]; then
                if [ ${usep["$fs"]} -gt ${usep1["$fs"]} ]; then
                        # Temp to store current percentage diff and partition to store in a variable.
                        tmp="$(echo -e "Running out of space \"$i (${usep["$fs"]}%%)\" on $(hostname) as on $(date)")";
                        echo "$tmp"
                        #tmp=$(printf "%s\n" "(${usep["$fs"]}%%) percent used on $i");
                        alertlist=("${alertlist[@]}" "$tmp")
                fi
        fi
done

if [ ${#alertlist} -gt 0 ]; then
	{ echo '<HTML><BODY>'
	  printf '%s<br />\n' "${alertlist[@]}"
	  echo '</HTML></BODY>'
	} | mail -s "ALERT:  Running out of space" $maillist
	echo "Following text sent as body of mail to $maillist:"
	printf '%s\n' "${alertlist[@]}"
fi

Sponsored Links
    #10  
Old Unix and Linux 1 Week Ago
viay viay is offline
Registered User
 
Join Date: May 2017
Last Activity: 23 May 2017, 8:25 AM EDT
Location: india
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
Yes. But we need more information.

With this latest version of your script we know that the text being sent is correctly formatted and that your recipient's mail reader is reformatting the text in the message before displaying it.

What application is being used by abc@gmail.com to read mail?

It uses MS Outlook,


Code:
If the mail reader being used is expecting HTML input, the thread Sendmail ignoring line endings
might provide the information we need to work around your problem by changing the line:

Yeah, I've gone through the thread Sendmail ignoring line endings and found that


Code:
The only one NOT adhering to this convention is perhaps your mail reader program (more correctly: mail user agent), which is probably Microsoft Outlook or similar crap. Because against all convention, laid down in RFCs, M$$ once decreed that email is not any longer plain ASCII text by default with MIME-parts in HTML being allowed, but in fact email is HTML from the start (to be precise: not exactly HTML, but what M$$ took as being being HTML, which wasn't quite up to the standard either).

then I tried with your script,


Code:
Code:
        printf '%s\n' "${alertlist[@]}" | mail -s "ALERT:  Running out of space" $maillist;

to something like:

Code:
        { echo '<HTML><BODY>'
          printf '%s<br />\n' "${alertlist[@]}"
          echo '</HTML></BODY>'
        } | mail -s "ALERT:  Running out of space" $maillist;

This is my updated script part

Code:
if [ ${#alertlist} -gt 0 ]; then
     #printf '%s\n' "${alertlist[@]}" | mail -s "ALERT:  Running out of space" $maillist;
        {
          printf '%s' "${alertlist[@]}\n"
        } | mail -s "ALERT:  Running out of space" $maillist;
      echo "Following text sent as body of mail to $maillist:"
      printf '%s\n' "${alertlist[@]}"
fi                     #alertlist holds the values to send mail.

I removed
Code:
echo '<HTML><BODY>'

Because it prints in mail,

the output is
Code:
 <HTML><BODY>
Running out of space "/u02 (94%)" on localhost.local as on Wed May 17  <br />Running out of space "/u05 (99%)" on localhost.local as on Wed May 17<br /></HTML></BODY>

am using MS Outlook 2013, what it does seems it prints all the line along <HTML>

---------- Post updated at 11:00 PM ---------- Previous update was at 10:58 PM ----------


Code:
export maillist=abc@gmail.com;
#df -PH /d04 /d05 /u01 /export | grep -vE '^Filesystem|none|cdrom'|awk '{ print $5 " " $6 }' | while read output;
df -PH | grep -vE '^Filesystem|none|cdrom|swdepot'|awk '{ print $5 " " $6 }' > $HOME/monitor/diskcheck.log;

unset usep usep1
declare -A usep usep1

if [ -s "$HOME/monitor/log/disk_alert.log" ]; then
  #Getting variables and compare with old
  while read -r pct fs
  do	pct=${pct%\%}
	usep["$fs"]=$pct
	printf '%s(%s) ' "$fs" "$pct"
  done < "$HOME/monitor/diskcheck.log"
  echo
  while read -r pct fs
  do	pct=${pct%\%}
	usep1["$fs"]=$pct
	printf '%s(%s) ' "$fs" "$pct"
  done < "$HOME/monitor/log/disk_alert.log"
  echo
else
  cp "$HOME/monitor/diskcheck.log" "$HOME/monitor/log/disk_alert.log"
  # Note that if disckcheck.log and disk_alert.log are identical, usep[$i] can
  #   never be greater than usep1[$i], so we might as well quit now.
  exit
fi

unset alertlist
for fs in ${!usep[@]}; do
        if [ ${usep["$fs"]} -gt 80 ]; then
                if [ ${usep["$fs"]} -gt ${usep1["$fs"]} ]; then
                        # Temp to store current percentage diff and partition to store in a variable.
                        tmp="$(echo -e "Running out of space \"$i (${usep["$fs"]}%%)\" on $(hostname) as on $(date)")";
                        echo "$tmp"
                        #tmp=$(printf "%s\n" "(${usep["$fs"]}%%) percent used on $i");
                        alertlist=("${alertlist[@]}" "$tmp")
                fi
        fi
done

if [ ${#alertlist} -gt 0 ]; then
	{ echo '<HTML><BODY>'
	  printf '%s<br />\n' "${alertlist[@]}"
	  echo '</HTML></BODY>'
	} | mail -s "ALERT:  Running out of space" $maillist
	echo "Following text sent as body of mail to $maillist:"
	printf '%s\n' "${alertlist[@]}"
fi

Thank you so much for sharing this kinda stuff, I would go implement this thing once the mailing part is succeed.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unable to pass shell script variable to awk command in same shell script Ashunayak Shell Programming and Scripting 2 01-27-2014 12:04 PM
Shell Script To E-Mail SalientAnimal Shell Programming and Scripting 14 11-16-2012 03:41 AM
How to send e-mail from shell script ( C shell )? arukuku UNIX for Dummies Questions & Answers 2 04-20-2012 06:19 AM
Mail shell script rgumm Shell Programming and Scripting 3 06-15-2010 05:35 AM
assign awk's variable to shell script's variable? tiger2000 Shell Programming and Scripting 4 04-13-2010 03:53 PM



All times are GMT -4. The time now is 04:49 AM.