Help with code

Dear Gurus , kindly take a look at this code

#!/bin/bash
declare -i total=0
declare -i frst=0
hr=`cat test2 | head -1 | cut -b12-13`
#echo $hr
for name in `cat test2`
do
hour=`echo $name | cut -b12-13`
#echo $hour
if [$hour == $hr]
then
#echo $name
frst=`echo $name | cut -f 4 -d ,`
total=`expr $total+$frst`
else
echo $total
declare -i total=0
declare -i frst=0
frst=`echo $name | cut -f 4 -d ,`
total=`expr $total+$frst`
fi
done

Algo ang logic seems to be correct , hr and hour are two strings and i m comparing them as you can see , but its not working somehow
second decalartion of total and frst is just to set them to zero again

Regards

Why? You are assigning them immediately afterwards....

The if statement syntax is wrong. Also, perhaps use bash for integer arithmetic instead of expr. Check the bash man pages for details of both.

What is the logic behind the script? The statements seems to repeat in the if loop. And i think there is no reason for the if loop since the operations are same in the then clause and the else clause.

Provide us more details

Well
here is the text of test2

2007-05-31-0000,0,0,537,538,489,490,0,0,0,0,0,0,46,46
2007-05-31-0001,0,0,552,552,489,489,2,2,0,0,0,0,60,60
2007-05-31-0002,0,0,526,526,482,482,0,0,0,0,0,0,43,43
2007-05-31-0003,1,1,575,575,518,518,0,0,0,0,0,0,57,56
2007-05-31-0004,0,0,522,522,480,480,0,0,0,0,0,0,42,42
2007-05-31-0005,0,0,533,533,479,479,0,0,0,0,0,0,53,53
2007-05-31-0006,0,0,489,489,442,442,0,0,0,0,0,0,47,47
2007-05-31-0007,0,0,504,504,473,473,0,0,0,0,0,0,31,31
2007-05-31-0008,0,0,485,484,451,451,0,0,0,0,0,0,34,33
2007-05-31-0009,0,0,475,475,442,442,0,0,0,0,0,0,33,33
2007-05-31-0010,0,0,506,507,461,461,0,0,0,0,0,0,45,46
2007-05-31-0011,0,0,485,484,450,450,0,0,0,0,0,0,35,34
2007-05-31-0012,0,0,485,486,434,434,0,0,0,0,0,0,51,52
2007-05-31-0013,0,0,447,446,411,410,1,1,0,0,0,0,35,35
2007-05-31-0014,0,0,471,472,423,424,1,1,0,0,0,0,46,46
2007-05-31-0015,0,0,445,445,398,398,0,0,0,0,0,0,47,47
.............
..............

well 2007-05-31-0015 is the date followed by time , as you can see it is updated every minute , so what i m trying to do is to sum fourth "," separated field ( italics ) for each , hour based on this algo
what algo is doing is summing all the values untill second character of time changes ( bold )
Problem is definitely in if statement , as i m comparing contents of two string variables , declaration of total and frst is just to flush their contents and set them back to zero
i hope its been clear now

Regards

Try and adapt the following script :

#!/usr/bin/bash
# Script File : test2.sh
# Input  File : test2.txt

declare -i total frst
while IFS=, read timestamp f2 f3 frst rest
do
   hour=${timestamp:11:2}
   : ${prv_hour:=$hour}
   if [ $hour != ${prv_hour} ]
   then
      echo "$hour => $total"
      (( total = 0 ))
      prv_hour=$hour
      hour=
   fi
   (( total += frst ))
done < test2.txt
[ -n "$hour" ] && echo "$hour => $total"

Input File:

$ cat test2.txt
2007-05-31-0000,0,0,537,538,489,490,0,0,0,0,0,0,46,46
2007-05-31-0001,0,0,552,552,489,489,2,2,0,0,0,0,60,60
2007-05-31-0002,0,0,526,526,482,482,0,0,0,0,0,0,43,43
2007-05-31-0003,1,1,575,575,518,518,0,0,0,0,0,0,57,56
2007-05-31-0004,0,0,522,522,480,480,0,0,0,0,0,0,42,42
2007-05-31-0005,0,0,533,533,479,479,0,0,0,0,0,0,53,53
2007-05-31-0006,0,0,489,489,442,442,0,0,0,0,0,0,47,47
2007-05-31-0007,0,0,504,504,473,473,0,0,0,0,0,0,31,31
2007-05-31-0008,0,0,485,484,451,451,0,0,0,0,0,0,34,33
2007-05-31-0009,0,0,475,475,442,442,0,0,0,0,0,0,33,33
2007-05-31-0010,0,0,506,507,461,461,0,0,0,0,0,0,45,46
2007-05-31-0011,0,0,485,484,450,450,0,0,0,0,0,0,35,34
2007-05-31-0012,0,0,485,486,434,434,0,0,0,0,0,0,51,52
2007-05-31-0013,0,0,447,446,411,410,1,1,0,0,0,0,35,35
2007-05-31-0014,0,0,471,472,423,424,1,1,0,0,0,0,46,46
2007-05-31-0015,0,0,445,445,398,398,0,0,0,0,0,0,47,47
2007-05-31-0100,0,0,537,538,489,490,0,0,0,0,0,0,46,46
2007-05-31-0101,0,0,552,552,489,489,2,2,0,0,0,0,60,60
2007-05-31-0102,0,0,526,526,482,482,0,0,0,0,0,0,43,43

Execution :

$ test2.sh
01 => 8037
01 => 1615
$

Jean-Pierre.

#!/bin/bash
declare -i total=0
declare -i frst=0
hr=`cat test2 | head -1 | cut -b12-13`
for name in `cat test2`
do
        hour=`echo $name | cut -b12-13`
        if [ "$hour" = "$hr" ]
        then
                frst=`echo $name | cut -f 4 -d ,`
                total=`expr $total+$frst`
        else
                echo "The sum of $hr hour = $total"
                declare -i total=0
                declare -i frst=0
                frst=`echo $name | cut -f4 -d,`
                total=`expr $total+$frst`
        fi
        hr=$hour
done
echo "The sum of $hour hour = $total"

Input file : test2

2007-05-31-0000,0,0,537,538,489,490,0,0,0,0,0,0,46,46
2007-05-31-0001,0,0,552,552,489,489,2,2,0,0,0,0,60,60
2007-05-31-0002,0,0,526,526,482,482,0,0,0,0,0,0,43,43
2007-05-31-0003,1,1,575,575,518,518,0,0,0,0,0,0,57,56
2007-05-31-0004,0,0,522,522,480,480,0,0,0,0,0,0,42,42
2007-05-31-0005,0,0,533,533,479,479,0,0,0,0,0,0,53,53
2007-05-31-0006,0,0,489,489,442,442,0,0,0,0,0,0,47,47
2007-05-31-0007,0,0,504,504,473,473,0,0,0,0,0,0,31,31
2007-05-31-0008,0,0,485,484,451,451,0,0,0,0,0,0,34,33
2007-05-31-0009,0,0,475,475,442,442,0,0,0,0,0,0,33,33
2007-05-31-0010,0,0,506,507,461,461,0,0,0,0,0,0,45,46
2007-05-31-0011,0,0,485,484,450,450,0,0,0,0,0,0,35,34
2007-05-31-0012,0,0,485,486,434,434,0,0,0,0,0,0,51,52
2007-05-31-0113,0,0,447,446,411,410,1,1,0,0,0,0,35,35
2007-05-31-0114,0,0,471,472,423,424,1,1,0,0,0,0,46,46
2007-05-31-0215,0,0,445,445,398,398,0,0,0,0,0,0,47,47

Output

$ ./test.sh
The sum of 00 hour = 6674
The sum of 01 hour = 918
The sum of 02 hour = 445