Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Correct way to add in ksh 2

Status
Not open for further replies.

jouell

MIS
Nov 19, 2002
304
US
Hi

I have the following to a compare numbers in ksh:

if [ $UTILIZED -gt $THRESHOLD ]; then

mail_me

fi

(I am monitoring df -k output)

Can I assume since the variable will contain a number it is treated as a numer? Or do i have to do something w/typeset to make it a number??


-john





 

On rare occasions you will have to add zero:

Code:
 (( UTILIZED += 0 ))



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

If a variable is the result of an execution assignment like the following example, it may have leading spaces and the test (if [ ...) will fail, therefore 'adding' zero will convert to number:

$ var=$(echo 123|awk '{printf "%9s\n", $1;}')
$ echo "($var)"
( 123)
$ (( var += 0 ))
$ echo "($var)"
(123)
$




----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
typeset -i UTILIZED THRESHOLD

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
OK I incorporated both and have submitted the script if people find it useful [it works =) ]

Code:
#!/bin/ksh
HOSTNAME=$(/usr/bin/hostname | /usr/bin/tr '[a-z]' '[A-Z]')
RECIPIENT="mygroup"
OTHER="pager@vtext.com"

#Leave number  in quotes
typeset -i THRESHOLD="95"

FILESYSTEMS=$(/sbin/mount | grep -iv proc|  grep -iv cdrom |  /usr/bin/awk '{print $3}') 

for FS in $FILESYSTEMS

do

 UTILIZED=$(/usr/bin/df -k $FS | /usr/bin/tail -1 | /usr/bin/awk '{print $5}' | cut -d"%" -f1)
 ((UTILIZED+=0))
 typeset -i UTILIZED

 SUBJECT="Filesystem $FS on $HOSTNAME is $UTILIZED% full!"
 MESSAGE="$SUBJECT Threshold is $THRESHOLD%. Immediate System Administrator intervention is required."

 # Define the email function:
 mail_alert ()
 {
 echo $MESSAGE | /usr/bin/mailx -s "$SUBJECT" "$RECIPIENT" "$OTHER"
 }



 # Compare the utilized value against the threshold:
 if [ $UTILIZED  -gt   $THRESHOLD ]; then

  mail_alert 

 fi

done

exit 0

 
Even though you are done, if you are using decimals I would suggest using ksh93 (dtksh).

float i

OR

float i=0

((i+=1))

Also, 'typeset -i' can be replaced with 'integer i=0' in ksh93.

Or even,
#!/usr/dt/bin/dtksh

float integer i=.3
float integer j=1.4
integer x=0

while (( x < 5 ))
do
((j+=i))
((x+=1))
done

print $j

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top