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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

String to int/float conversion

Reply to Message Icon

Name: sathyaprasad
Date: June 25, 2007 at 02:36:44 Pacific
OS: Solaris
CPU/Ram: Sun
Product: 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



Sponsored Link
Ads by Google

Response Number 1
Name: lankrypt0
Date: June 25, 2007 at 09:28:14 Pacific
Reply:

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


0

Response Number 2
Name: nails
Date: June 25, 2007 at 09:33:54 Pacific
Reply:

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


0

Response Number 3
Name: sathyaprasad
Date: June 26, 2007 at 10:40:28 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Script to Lock BSC getopts exclusive argumen...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: String to int/float conversion

Script to scan files for a string www.computing.net/answers/unix/script-to-scan-files-for-a-string/7532.html

to calculate column total www.computing.net/answers/unix/to-calculate-column-total/3992.html

string to number www.computing.net/answers/unix/string-to-number/5537.html