Computing.Net > Forums > Unix > string to number

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

string to number

Reply to Message Icon

Name: George
Date: September 10, 2003 at 10:47:50 Pacific
OS: sun
CPU/Ram: 1024
Comment:

In the following script when I get SIZE1 and SIZE2, they are of type string. I can not do any mathematical operations(especially comparing which file has more byte size) on them. I wanted to find out if I can convert string in to Number. Thanks in Advance.

FILE1='/export/registry/incoming/reg.dmp'
FILE2='/export/registry/reg.dmp'

SIZE1=`ls -l $FILE1 | awk '{print $5}'`
SIZE2=`ls -l $FILE2 | awk '{print $5}'`

if [ $SIZE1 > $SIZE2 ]
then
echo $SIZE1 - $SIZE2
else
echo "The 1st file is smaller."
fi




Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: September 10, 2003 at 12:50:30 Pacific
Reply:

Use the operators -ge, -lt, -eq, -ne for integer comparisons.


0

Response Number 2
Name: zeroguy
Date: September 10, 2003 at 14:31:22 Pacific
Reply:

Just to be perfectly clear in case there is a little confusion: -qe is 'greater than or equal to', -lt 'less than', -eq 'equal to', -ne 'not equal'.


0

Response Number 3
Name: zeroguy
Date: September 10, 2003 at 14:32:21 Pacific
Reply:

Sorry, that first one should be -ge, not -qe


0

Response Number 4
Name: WilliamRobertson
Date: September 10, 2003 at 16:18:51 Pacific
Reply:

In ksh, zsh and bash you can use arithmetic expressions, e.g:

if (( SIZE1 > SIZE2 ))
then
print "$SIZE1 is larger than $SIZE2"
else
print "The 1st file is smaller."
fi

This also gives you a standard set of operators, >=, +=, ++, -- etc.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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 number

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

Replace a string in Script www.computing.net/answers/unix/replace-a-string-in-script/7047.html

parsing a variable to a function www.computing.net/answers/unix/parsing-a-variable-to-a-function-/5613.html