Why does my shell and subshell only print the first hour in my for loop? It's supposed to loop through all 24 hours in a day but only prints the first hour correctly.
{
for D in $(seq -f "%02g" 1 9); do
File=($(find -name "PaloAlto_traffic_$M-$D-$Y.zip"))
unzip -j $File
Filetmp=($(find -name "traffic_$M-$D-$Y.total"))
D=$(echo $D | sed -e 's/^0*//')
for i in $(seq -f "%02g" 0 23); do
echo $i
grep -F -s "$month $D $i:" $Filetmp > TempOutput1.txt
countAllow=($(grep -F -c 'allow' TempOutput1.txt))
countDeny=($(grep -F -c 'deny' TempOutput1.txt))
#Add new 'variable=($(grep)) statement here'
echo -e "$M/0$D/$Y $i:00,$countAllow,$countDeny" >> AD-Results-$month-$Y
echo "$((10#$i+1))/24 hours completed for $month $D"
rm -f $Filetmp
done
done
} & #Subshell 2A
{
for D in $(seq -f "%02g" 10 18); do
File=($(find -name "PaloAlto_traffic_$M-$D-$Y.zip"))
unzip -j $File
Filetmp=($(find -name "traffic_$M-$D-$Y.total"))
for i in $(seq -f "%02g" 0 23); do
grep -F -s "$month $D $i:" $Filetmp > TempOutput2.txt
countAllow=($(grep -F -c 'allow' TempOutput2.txt))
countDeny=($(grep -F -c 'deny' TempOutput2.txt))
#Add new 'variable=($(grep)) statement here'
echo -e "$M/$D/$Y $i:00,$countAllow,$countDeny" >> AD-Results-$month-$Y
echo "$((10#$i+1))/24 hours completed for $month $D"
rm -f $Filetmp
done
done
}
wait
Sample output data: (Date Time , Allow , Deny)
05/10/2014 00:00,3242,6758
05/10/2014 01:00,0,0 #This should be the same as above line but outputs 0,0
You probably remove the file named by
$Filetmp
too soon. Therm
command is inside the inner loop:Due to this, the file is removed after the first iteration through the inner loop (i.e. when $i = 0). Subsequent iterations ($i > 0) will not find the file. No error is reported because you call grep with option
-s
, but the resulting counts are obviously zero.The
rm
command should be outside the inner loop: