Searching for variable string

Hi Guys,

So im trying to search for the most recent occurance of a string in the most recently updated .log file in a certain directory. The string i am searching for is a value, now if this value is greater than 800 i would like an email sent out with some text (blah blah blah).

This is what i have so far

 
ls -Art | tail -n 1| xargs tac | grep -m1  "Total number of clients: "

Running this command will output the following

19:55:51.815 Total number of clients: 338
xargs: tac: terminated by signal 13

As you can see the total number of clients is smaller than 800, so we dont need an email notification sent out. But in the instance of the value being greater than 800 we will need a notifcation sent.

Your help is appreciated.

Try this using awk:

fname=$(ls -Art | tail -n 1) 
awk  '/Total number of clients: / && $(NF)>800 {print $0}' $fname > tmp.tmp
[ -s tmp.tmp ] && mailx -s 'blah blah' me@mycompany.com < tmp.tmp

For "the most recent occurance", you might want to adapt jim mcnamara's proposal like

awk  '/Total number of clients: / && $(NF)>800 {T=$0} END {print T}' $fname > tmp.tmp

I need to make this as a cronjob that runs every 3 minutes however, this seems to send an email out regardless if the value is larger or smaller than 800. I need it ONLY to email if the value is larger than 800, any ideas?

Cheers

What operating system are you using?

Are you specifying the shell to be used in your script? (If so, how?)

When you are running your script with cron , how are you setting your script's environment variables and the directory in which your script is run?

In what directory are you hoping to find the file(s) you want to process?

  • Linux 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

*I created a shell script and gave it execute access E.g. ./connectioncount.sh

*I havent set up the cron yet, once i get the shell script working i will implement as a cronjob

*The directory is in Genesyslogs\confserv

Hope that answers your questions.

Reading in, ive decided to rewrite the script as i prefer to use tac rather than awk as the file is quite large and the job will need to run every 3 minutes. Im half way there, Please see below

#!/bin/bash
fname=$(ls -Art | tail -n 1)
echo $fname
value=$(tac $fname | grep -m1 "Total number of clients:")
echo $value

All i need now is to capture the last numeric values which appear after Total number of clients: xxxx

If this value is greater than 800 than an email will be sent out.

Thanks.

---------- Post updated at 10:25 PM ---------- Previous update was at 10:24 PM ----------

Almost there, though i cannot seem to get the if statement working, it hangs on that particular command and i dont receive an email. Did i use the correct syntax?

#!/bin/bash
fname=$(ls -Art | tail -n 1)
echo $fname
value=$(tac $fname | grep -m1 "Total number of clients:")
echo $value
variable=${value##* }
echo $variable
if [ $variable -lt 800 ]; then
	mailx -s "vpsi1ftc has exceeded the connection: $variable" mycompany@company.com.au
fi

Take a look at the red highlighted. If your shell supports it you may do

if [[ variable -lt 800 ]]

or

if (( variable < 800 ))

However you have been mentioning all along:

Thus, the proper comparison should be:

if [ "$variable" -gt 800 ]

Hi!

Was using the -lt parameter for testing purposes but heres what i ended up with, seems to work great! THanks for your help

fname=$(ls -Art | tail -n 1)
echo $fname
value=$(tac $fname | grep -m1 "Total number of clients:")
echo $value
variable=${value##* }
echo $variable
if [ $variable -gt 800 ]; then
        echo "" | mail -s "WARNING: vpsi1ftc Configuration Server Connection Count: $variable" email@company.com
fi