svk
July 24th, 2008, 07:59 AM
Hello,
I'm very new to shell scripting in Bash.
I'm trying to figure out how to treat the output of stat -c %s filename as a number, and not as a string. If you run that command, it will output the size of the file in bytes.
Here's the script I'm working on. It iterates over every file in the working directory, and attempts to print the cumulative size of all the files over which it iterated so far.
#!/bin/bash
# print-cumulative-sizes.sh
declare SIZE=0
declare TOTAL=0
for file in *; do {
SIZE=`stat -c %s $file`;
TOTAL=`expr $SIZE+$TOTAL`;
echo $TOTAL;
}; done
The problem should be evident from the output:
serg@bijou:~/Pictures$ ./print-cumulative-sizes.sh
1036228+0
970325+1036228+0
1108950+970325+1036228+0
1476318+1108950+970325+1036228+0
1027836+1476318+1108950+970325+1036228+0
1050479+1027836+1476318+1108950+970325+1036228+0
1156703+1050479+1027836+1476318+1108950+970325+103 6228+0
1215533+1156703+1050479+1027836+1476318+1108950+97 0325+1036228+0
960132+1215533+1156703+1050479+1027836+1476318+110 8950+970325+1036228+0
1569659+960132+1215533+1156703+1050479+1027836+147 6318+1108950+970325+1036228+0
1149074+1569659+960132+1215533+1156703+1050479+102 7836+1476318+1108950+970325+1036228+0
4096+1149074+1569659+960132+1215533+1156703+105047 9+1027836+1476318+1108950+970325+1036228+0
98528+4096+1149074+1569659+960132+1215533+1156703+ 1050479+1027836+1476318+1108950+970325+1036228+0
142+98528+4096+1149074+1569659+960132+1215533+1156 703+1050479+1027836+1476318+1108950+970325+1036228 +0
142+142+98528+4096+1149074+1569659+960132+1215533+ 1156703+1050479+1027836+1476318+1108950+970325+103 6228+0
See what's wrong? How can I get bash to treat my $SIZE and $TOTAL variables as numbers, not as strings?
Thank you in advance for any help!
I'm very new to shell scripting in Bash.
I'm trying to figure out how to treat the output of stat -c %s filename as a number, and not as a string. If you run that command, it will output the size of the file in bytes.
Here's the script I'm working on. It iterates over every file in the working directory, and attempts to print the cumulative size of all the files over which it iterated so far.
#!/bin/bash
# print-cumulative-sizes.sh
declare SIZE=0
declare TOTAL=0
for file in *; do {
SIZE=`stat -c %s $file`;
TOTAL=`expr $SIZE+$TOTAL`;
echo $TOTAL;
}; done
The problem should be evident from the output:
serg@bijou:~/Pictures$ ./print-cumulative-sizes.sh
1036228+0
970325+1036228+0
1108950+970325+1036228+0
1476318+1108950+970325+1036228+0
1027836+1476318+1108950+970325+1036228+0
1050479+1027836+1476318+1108950+970325+1036228+0
1156703+1050479+1027836+1476318+1108950+970325+103 6228+0
1215533+1156703+1050479+1027836+1476318+1108950+97 0325+1036228+0
960132+1215533+1156703+1050479+1027836+1476318+110 8950+970325+1036228+0
1569659+960132+1215533+1156703+1050479+1027836+147 6318+1108950+970325+1036228+0
1149074+1569659+960132+1215533+1156703+1050479+102 7836+1476318+1108950+970325+1036228+0
4096+1149074+1569659+960132+1215533+1156703+105047 9+1027836+1476318+1108950+970325+1036228+0
98528+4096+1149074+1569659+960132+1215533+1156703+ 1050479+1027836+1476318+1108950+970325+1036228+0
142+98528+4096+1149074+1569659+960132+1215533+1156 703+1050479+1027836+1476318+1108950+970325+1036228 +0
142+142+98528+4096+1149074+1569659+960132+1215533+ 1156703+1050479+1027836+1476318+1108950+970325+103 6228+0
See what's wrong? How can I get bash to treat my $SIZE and $TOTAL variables as numbers, not as strings?
Thank you in advance for any help!