Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
!/bin/bash
number1=0
num=0
for file in * ;do (cd $file;
for a in *.grb ;do
expr $num + 1 > temp_num
read num < temp_numecho $num
done) ;
expr $number + 1 > temp_number
read number < temp_number
expr $number1 + $num > temp_number1
read number1 < temp_number1
echo $number1done
In the above script I just want to count how many times the inner loop loops and then add what is counted to the outer loop. And goes through all the loops to get the final counts both the inner and the outer loops.The problem with the above is that the inner loop does count correct, but as soon as it goes to the outer loop it assigned a zero value to ($num). How can I keep the value of $num so that it will be added to the outer loop.
How can I fix this ?
Yacob

Use two numbers, one for each loop.
???I read it wrong and answer it wrong too. So get off my case you peanut.

#!/bin/ksh
# instead of using expr you can perform integer arithmetic using double parenthesis:
num=0((num=num+1))
echo $num # = 1# or
((num+=1))
echo $num # = 2
# this is illegal
# expr $num $num + 1 > temp_num# if you want to send the variable to a file, do this:
echo $num > temp_num
# Here's the problem: placing commands inside parenthesis, as you've done, forces spawning
# another shell. Variable num equals 6 when the for loop completes, but when the shell dies
# the value num returns to 0. Below, remove the parenthesis around the for loop, and you'll
# see what I mean:
num=0
echo $file
(for i in 1 2 3
do
((num=num+i))
done
echo $num)echo $num

Dear Nails
Thank you for your comments, and it was helpful. However, I have still a problem where the "num1" value assigned to a zero when it finishes the outer for-do loop. Here is the script.
For dec in * ;do (cd $dec;
For a in *.grb ;
do
((num=num + 1 ))
echo $numdone ;
((num1= num1 + num ))echo $num1)
read pause
done
Still "num1" gets zero for the outer "for" loop. The "num" does now transport the correct value as I wished when it comes out from the inner for loop. But the "num1" does loose when the outer for loop runs the second time.As you can see, the outer loop reads a "directory" name and the second loop "cd" to that directory and goes through all the file and in that directory and I apply all sort of manupulations to the file and the loop goes on until all the directory is exhausted.

Thank you for the compliment, but you are ignoring what I said about spawning another shell with parenthesis:
# Here's the problem: placing commands inside parenthesis, as you've done, forces spawning
# another shell. Variable num equals 6 when the for loop completes, but when the shell dies
# the value num returns to 0.Consider this example. the value of num = 12 when the loops complete:
#!/bin/bashnum=0
for x in 1 2
do
for i in 1 2 3
do
((num=num+i))
done
done
echo $numNow, enter the parenthesis and see that the variable resets to 0:
#!/bin/bashnum=0
for x in 1 2
do
(for i in 1 2 3
do
((num=num+i))
done)
done
echo $numThere are two things you can do:
1) Remove the parentheses from your script. I see no reason for them.
2) If you insist on the prentheses, redirect the num to a file, and then read the file when the for loops complete.

![]() |
Question on setup faxing ...
|
Would it be possible?
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |