Operating System - HP-UX
1827207 Members
2775 Online
109716 Solutions
New Discussion

Re: ksh: max value of integer?

 
SOLVED
Go to solution
L Tattrie
New Member

ksh: max value of integer?

Hi,
When files can be larger than 2Gb can the size still be assigned to a ksh integer? What is the maximum number a ksh variable can hold?

On some of the servers the gzip has not be ugraded to handle files larger than 2Gb so I want part of a script to do an error check and compare the size of a file to 2,147,283,648. I have been looking around a lot but have not come across any information about using larger numbers in shells. Do the companies which have very large files have to use another language such as perl or tcl to be able to user numbers larger than 2,147,283,648 or 4,294,967,296 (4 Gb)?
5 REPLIES 5
John Palmer
Honored Contributor
Solution

Re: ksh: max value of integer?

Hi,

Posix shell is 2^32-1 (2147283648).

You could use the output of du -k which is in kilobytes to do your test in the shell rather than the actual size in bytes.

Regards,
John
L Tattrie
New Member

Re: ksh: max value of integer?

Much thanks John. That is exactly what I asked plus what I needed to know.
- Lawrence
James R. Ferguson
Acclaimed Contributor

Re: ksh: max value of integer?

Hi:

You can use 'bc' in a shell to do arithmetic that involves numbers greater than 2^32-1 and/or to do real (not integer) arithmetic:

# X=1;Y=8;echo "scale=3\n $X/$Y"|bc

# echo "(2^33-1)/3"|bc

The output can be examined as a string and passed (again) to 'bc' for further calculation.

Regards!

...JRF...
L Tattrie
New Member

Re: ksh: max value of integer?

Thanks for the quick accurate info. This tiny script shows the problem of large integers when a file using more than 2gb is passed to the script.
#!/usr/bin/sh
FILE=$1
print "Use du -k to get size"
TWO_GB=2096956
KB=`du -k $FILE | awk '{print $1}'`
print "KB is $KB TWO_GB is $TWO_GB"
if [[ $KB -gt $TWO_GB ]]
then
print "File is more than 2 gb"
else
print "File is less than 2 gb"
fi
print "Use actual file size "
FSIZE=`ls -l $FILE | awk '{print $5}
print "FSIZE is $FSIZE "
if [[ $FSIZE -gt 2147283647 ]]
then
print "File is more than 2 gb"
else
print "File is less than 2 gb"
fi
This gave results of
Use du -k to get size
KB is 2635792 TWO_GB is 2096956
File is more than 2 gb
Use actual file size
FSIZE is 2699042816
File is less than 2 gb

The use of bc is interesting but I could not set a return code for true or false.
As disk drives get larger so will the use of arbitrary length integers.
Bill Hassell
Honored Contributor

Re: ksh: max value of integer?

You probably want a copy of bdfmegs, a script that shows disks in megs (much more practical with today's disks). Get a copy at:

ftp://contrib:9unsupp8@hprc.external.hp.com/sysadmin/coolscripts/

Netscape and Opera work fine with an ftp URL but newer Internet Exploders will hang if the folder view is enabled (which is the dumb default). Turn it off in the Tools->Internet->Advanced menu.


Bill Hassell, sysadmin