Computing.Net > Forums > Unix > String to int/float conversion

String to int/float conversion

Reply to Message Icon

Original Message
Name: sathyaprasad
Date: June 25, 2007 at 02:36:44 Pacific
Subject: String to int/float conversion
OS: Solaris
CPU/Ram: Sun
Model/Manufacturer: Sun
Comment:

Hello all, I am trying to read the 5th column of a log file which is the percentage filled space for a logical drive. I am writing a script which throws up a warning if this is greater than 60. But j is taken as a string and when I compare with 60 in the if statement, it does not work. How can I change string/ascii to int or float in shell so that the comparison works?

j=`echo $line | tr -s ' ' ' ' | cut -d' ' -f5`
if [ $j -gt 60 ]
then
echo "WARNING:$line"
fi


Report Offensive Message For Removal


Response Number 1
Name: lankrypt0
Date: June 25, 2007 at 09:28:14 Pacific
Subject: String to int/float conversion
Reply: (edit)

Can you give an example of the lines in the file?


Report Offensive Follow Up For Removal

Response Number 2
Name: nails
Date: June 25, 2007 at 09:33:54 Pacific
Subject: String to int/float conversion
Reply: (edit)

I would say the problem is the way variable $j is created because this works on my solaris 9:

#!/bin/sh

j="59"
if [ $j -gt 60 ]
then
echo "greater than 60"
fi


Report Offensive Follow Up For Removal

Response Number 3
Name: sathyaprasad
Date: June 26, 2007 at 10:40:28 Pacific
Subject: String to int/float conversion
Reply: (edit)

while read line
do
set -f
set -- $line
j=$5
if [ $j -gt 60 ]
then
echo "Value of j is $j"
echo "WARNING:$line"
fi
done < dblist1

Found out ... This is working


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: String to int/float conversion

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge