Sun Solaris Unix Shell script that adds 2 to a string numeric and checks last value for change before emailing

Currently our Sun E450 could detect room temperature (from "prtdiag -v" and I've
observed that it's consistently reporting about 1.5-2.0 Celsius lower than the
general DC room temperature :

# prtdiag -v | grep -i ambie

"date" command in this Solaris box's format is :
# date
Sat May  9 06:06:58 SGT 2009

I'll need a Shell script that run every 1 minute (I'll put this into the crontab as
1,2,...59 * * * * /path/script_name) that will do the following :

I'll need this Shell script to be able to notify/email me when room temperature
(>= 21  AND there's a change from the last value)   or
(<= 20  AND the temperature recorded one minute ago is >= 21)

Above script basically notifies me once only (not repeatedly) when temperature
hits 21 C  but if temperature keep going up (at levels above 21 C) it will keep emailing/
notifying me.
This script only notify me once when temperature go below 21 C (but only notify
once and not repeatedly and it will stop notifying if room temperature stays below
20 C)  as the pseudo codes below describe :

Currentdatetime=yyyymmdd:hhmm   (24 hr format)
echo RoomTemp + " " + Currentdatetime  >> yyyymmdd_outputfile
add 2 to the value given by "prtdiag -v | grep AMBIENT" (call this variable RoomTemp
  if RoomTemp equals or exceeds 21
    check RoomTemp vs last_temperature recorded in last line of yyyymmdd_outputfile
        if RoomTemp equal or exceed 21 AND is higher than last_temperature
           email to myemail the value Currentdatetime+" "+":Room temp is now "+RoomTemp
  if last_temperature in last line of yyyymmdd_outputfile >= 21 and RoomTemp is <=20
    email to myemail the value Currentdatetime+" "+":Room temp now is down to "+RoomTemp

Who is Participating?

Improve company productivity with a Business Account.Sign Up

ahoffmannConnect With a Mentor Commented:
#! /bin/sh
[ -e $last ] &&  last_temp=`cat $last`
temp=`prtdiag -v |awk '/AMBIENT/{print $2}'`
echo $tmp >$last
[ $temp -ge 21 ] && mail -s21+ me@some.where
[ $temp -le 20 -o $last -ge 21 ] && mail -slast21+ me@some.where
exit 0
# not that the script relies on your cron to satisify your "one minute ago" condition
docbertConnect With a Mentor Commented:
ahoffmann's script misses one of your requirements - that it only sends a mail when "there's a change from the last value". As it is it will continue to email you once a minute until the temperature drops.

You can add the following below the "echo" statement to add that requirement :

[ $temp -eq $last_temp ] && exit

The 2nd last line is also wrong - it's using a -o (or) where it should be and, plus it's using $last rather than $last_temp.  I'd replace this line with :

[ $temp -le 20 ] && [ $last_temp -ge 21 ] && mail -slast21+ me@some.where
arober11Connect With a Mentor Commented:
On the crontab front, you dont need:

1,2,...59 * * * * /path/script_name

As the following are equivalent:

0-59 * * * * /path/script_name

* * * * * /path/script_name

ahoffmannConnect With a Mentor Commented:
> As the following are equivalent:
no, except you mean that a vixie cron is equivalent to a vixie cron ;-)
arober11Connect With a Mentor Commented:
Well yes, using either variant above, assuming your using a cron daemon that supports Paul Vixie 1987 syntax, will save having to add an entry along the lines :

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,34,35,36,37,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 * * * * /path/script_name

You might also want to look at redirecting the output from the cron, to save the output being mailed to owning user, by appending something like this to the line:

 >/var/log/some_log_file 2>&1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.