Results 1 to 7 of 7
Hi,
The check on the size of a file I perform in a tcsh script does not work.
The size of my file is the following :
ls -l File.gpg
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 05-07-2011 #1Just Joined!
- Join Date
- Oct 2006
- Posts
- 33
File size not properly checked in a tcsh script.
Hi,
The check on the size of a file I perform in a tcsh script does not work.
The size of my file is the following :
ls -l File.gpg
-rw-r--r-- 1 xuo users 3354637392 2011-05-07 15:31 File.gpg
The script is the following :
#!/bin/tcsh -f
set listOfEncryptFiles = `ls *.gpg`
foreach file ($listOfEncryptFiles)
set fileSize = `stat -c %s $file`
echo $fileSize
if (${fileSize} > 240000000) then
echo "Size of ${file} is bigger than 240000000"
endif
end
When I run it, I get :
3354637392
instead of :
3354637392
Size of File.gpg is bigger than 240000000
In other words, the script considers the size of the file to be less than 240M.
For other file bigger than 240M but smaller than 3G, the script works. At least, up to 2138603520, it works. With a file size of 2566324224, it does not.
My linux version is a Mandriva 32bits. My PC is a 64bits one.
Is there a way to bypass this problem ?
Regards.
Xuo.
- 05-07-2011 #2Just Joined!
- Join Date
- Apr 2011
- Posts
- 19
Guessing 32 bit arithmetic
I don't do much scripting but I am guessing that tcsh is using 32 bit ints for numbers. Your 3.4G number exceeds the size of an 32 bit integer.
- 05-07-2011 #3Just Joined!
- Join Date
- Apr 2011
- Posts
- 19
find
You could probably use find to get a list of the large files instead. Alternatively, if you can live with file allocation instead of file size being more than 240M, you could use the file's block allocation instead, which would typically max you out at 512*2.1G. You'll need to verify its 512 byte blocks on the filesystem though.
- 05-07-2011 #4Just Joined!
- Join Date
- Oct 2006
- Posts
- 33
Hi,
1) 2**32 = 4G which is bigger than 3G. I thought this was not a limitation of integers description in tcsh.
2) I did what you suggested. Working which blocks number instead of file size bypasses the problem.
The new script is below :
#!/bin/tcsh -f
set listOfEncryptFiles = `ls *.gpg`
set refSize = 240000000
foreach file ($listOfEncryptFiles)
set blockNumber = `stat -c %b $file`
set blockSize = `stat -c %B $file`
set refBlockNumber = `echo "${refSize}/${blockSize}" | bc`
if (${blockNumber} > ${refBlockNumber}) then
set fileSize = `echo "${blockNumber} * ${blockSize}" | bc`
echo "Size of ${file} is ${fileSize} which is bigger than ${refSize}."
endif
end
Thank you for your help.
Regards.
Xuo.
- 05-09-2011 #5
Or you cast the file size to a unsigned long, which should be possible in csh.
- 05-10-2011 #6Just Joined!
- Join Date
- Oct 2006
- Posts
- 33
Hi,
If it is possible, I don't know how to do it.
Regards.
Xuo.
- 05-11-2011 #7
Not with csh directly, but a combination of tools should do it:
* awk | wc
* grep
as those tools allow you to handle numbers as strings and compare them as such (provided as-is and written down swiftly in this little message window
):
The lexicographical comparison may has to be refined with awk. I'm in the end not that familiar with csh.Code:# make sure a and b are numbers a = `grep [1-9][0-9]* < "1001"` b = `grep [1-9][0-9]* < "1001"` # get the length aLength = `wc -c < $(a)` bLength = `wc -c < $(b)` if($(aLength) < $(bLength)) { # a smaller } else if ($(aLength) > $(bLength)) { # b smaller } else { # equal length, compare lexicographically if ($(a) < $(b)) { # a smaller } else if($(a) > $(b)) { # b smaller } else { # a and b are equal } }


Reply With Quote
