Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want a script which increments the count when the script runs. Basically I want to send an email notification when I find 10 devices down from the same country and scripts run after every down event.

So if I set the counter as 0, the script then updates the value as 1 but next time when the script runs it checks the counter is set to 0 and again shows the value as 1.

I need to save the previous counter value with respect to thee country name as both the values are not fixed. Also there are n number of devices belong to n number of countries.

share|improve this question
    
you can also set environment variable if possible eg export var=1 in the terminal and you can use var=$((var+1)) export var echo $var in a file example mailit.sh then when you give source mailit.sh it works as counter –  harish.venkat Nov 30 '12 at 17:49
add comment

2 Answers

Hopefully this answer will be a bit clearer. One file/counter per country.

#!/bin/bash
#Country name is specified as a comamnd line argument
# ie device_count.sh Brazil
if [[ -z "$1" ]] ; then
   echo "Please specify country name" >&2
   exit 1
fi

#Create a new file per country if one doesn't exist already
COUNTER_FILE=/var/tmp/devices.$1
if [[ -r $COUNTER_FILE ]] ; then
   COUNT=$(<$COUNTER_FILE)
else
   COUNT=0
fi

#Increment counter and save to file 
echo $(( $COUNT += 1 )) > $COUNTER_FILE

#check if we need to send email
if [[ $(( $COUNT % 10 )) -eq 0 ]] ; then
   #We have reached 10 - we need to send an email
   echo "BLAH BLAH BLAH " | mailx -s "reached 10" you@domain.com
fi
share|improve this answer
    
Thanks Darkheart... What -eq 0 stands for as I am looking for to reset counter to 0 once it sends an email. Will this help ? –  user28104 Dec 2 '12 at 15:19
    
The '%' (aka 'modulo') will divide the counter by 10 and give the remainer. So, therefore, each time the counter reaches a multiple of ten there will be no remainer (ie 0) and an email will be sent. –  DarkHeart Dec 4 '12 at 8:48
add comment

You need to write the country counts to a file before you exit your script as the last thing. The next time you run your script, you need to read these values from the same file. Otherwise you can not keep the values in memory variables as each shell script you run, executes a subshell with its own variables and destroys the shell and contents, when it exits.

x="$country" 

count=$(cat ${country}) 
#instead of starting from 0 each time, start from the content of this file
#you need to manually create each country file with value 0 in it
#before start using this struct.

for device_count in $x 
do 
count=expr $count + 1 
echo "Country_[$device_count] count $count" 
if [ count -eq 5 ]; 
then 
echo "email to be sent " 
fi 
done 


echo ${count} > ${country} 
#at this point you overwrote the file named as
#the name of country you are working here
share|improve this answer
    
Thanks for the reponse but its not clear to me ....Could you please explain on script...for example we have already i dentified country for respective down event now that is the only input ,so if 5 down event from same country... x="$country" count=0 for device_count in $x do count=expr $count + 1 echo "Country_[$device_count] count $count" if [ count -eq 5 ]; then echo "email to be sent " fi done –  user28104 Nov 30 '12 at 17:35
    
Please see above. –  Mel_Burslan Nov 30 '12 at 17:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.