Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-06-2013, 11:12 AM   #1
Registered: Nov 2011
Posts: 69

Rep: Reputation: Disabled
Smile echo output of command and send to email address

Ok below is the code to send the everything works except the following please see code

count=$(mailq | grep "Total requests: "| awk '{print $3}')
actualcount=$(ls /var/spool/mqueue | wc -l)
emailvar=`ls -al /var/spool/mqueue| sort $5 |awk '{print $5}'| uniq -c| sort $1| tail -1 |awk '{print $2}'`
emailbody=$(ls -al /var/spool/mqueue | grep $emailvar | awk '{print $9}'| head -1)
emailcat=$(cat $emailbody)

if [ $count -ge 50 ]
echo -e "Mail queue is at: $count" "Actual count of mail que directory is:"$actualcount "Please check mailq directory for file size of $emailvar" $emailc

        if [ $actualcount -gt 300 ]
        rm /var/spool/mqueue/qf*
        rm /var/spool/mqueue/df*
and when I run the command I get the following error I did get this to work once but then lost the file

"/usr/bin/mailcount" 19 lines, 650 characters 
bash-2.05# mailcount
cat: cannot open qfr42JVHKB027814
Old 05-06-2013, 11:22 AM   #2
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,250

Rep: Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432Reputation: 4432
...and unless you run it as root, you're not going to be able to open anyone else's mail but your own.
Old 05-07-2013, 02:38 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
That code is not particularly clean.

I see Useless Use Of Grep and long chains of text tools that could be replaced by simpler awk versions. Except that said commands are also ill-advisedly parsing ls! Not good.

And since this is a bash script, the numerical tests should be using ((..)) instead.

I tried to do a bit of re-writing, but I'm stuck over figuring out what two "ls -al /var/spool/mqueue" lines are supposed to produce. I don't have that directory on my system. I tried to reverse engineer them, but it's just not clear to me what they're doing exactly.

I can already say, at least, that "sort $5" and "sort $1" won't work properly, unless those are values input when the script is called, since those will be treated as shell positional parameters. sort is not awk. I imagine you want "sort -n k5,5" instead. The proper syntax for tail is also "tail -n 1". The "-1" style is deprecated.

But again, I'm sure there are better ways to get that data anyway.

Anyway, here's what I came up with far:



count=$( mailq | awk '/Total requests:/ {print $3}' )
mqlist=( /var/spool/mqueue/* ) ; actualcount=${#mqlist[@]}

#can't fix these two yet (except for sort), but they really need it.
emailvar=$( ls -al /var/spool/mqueue | sort -k5,5n | awk '{print $5}'| uniq -c | sort -k1,1n | -tail -n 1 | awk '{print $2}' )
emailbody=$( ls -al /var/spool/mqueue | grep $emailvar | awk '{print $9}'| head -1 )

emailcat=$( <"$emailbody" )

if (( count >= 50 )); then

    mail "$mailaddress" <<-MAILBODY
	Mail queue is at: $count
	Actual count of mail que directory is: $actualcount
	Please check mailq directory for file size of $emailvar

    if (( actualcount > 300 )); then
        rm /var/spool/mqueue/[qd]f*


exit 0
Note the use of '<<-' in the here document. The extra '-' means that you can indent the body of it with initial tab characters, to help with script formatting. These will be removed during execution and won't be included in the mail itself. They must be tabs though, not spaces!
1 members found this post helpful.


email, script, unix

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
echo two command output in the same line chuikingman Linux - Software 8 03-31-2011 07:20 PM
Command based email client to send email through secure smtp havolinec Linux - Newbie 2 07-27-2010 08:40 AM
[SOLVED] allow unsubscribed email address to send email to mailman list umarzuki Linux - Server 1 07-01-2010 04:54 AM
Ned to send command output to email Thaidog Programming 2 11-21-2009 12:58 PM
how to send the output of cronjobs to another email address ? kirtikjr Linux - Software 1 07-12-2007 06:36 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:21 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration