LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 05-06-2013, 10:12 AM   #1
slufoot80
Member
 
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

Code:
#!/bin/bash
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 ]
then
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
at|mail frank.nowicki@hovservices.com


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

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

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
...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, 01:38 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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:

Code:
#!/bin/bash

mailaddress='frank.nowicki@hovservices.com'

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
	$emailcat
	MAILBODY

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

fi

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.
  


Reply

Tags
email, script, unix



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 11 05-06-2019 10:44 AM
Command based email client to send email through secure smtp havolinec Linux - Newbie 2 07-27-2010 07:40 AM
[SOLVED] allow unsubscribed email address to send email to mailman list umarzuki Linux - Server 1 07-01-2010 03:54 AM
Ned to send command output to email Thaidog Programming 2 11-21-2009 11:58 AM
how to send the output of cronjobs to another email address ? kirtikjr Linux - Software 1 07-12-2007 05:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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