Computing.Net > Forums > Linux > correct count in the bash script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

correct count in the bash script

Reply to Message Icon

Name: yacob
Date: March 21, 2008 at 12:32:27 Pacific
OS: Linux
CPU/Ram: 2GB
Comment:

!/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_num

echo $num

done) ;

expr $number + 1 > temp_number
read number < temp_number


expr $number1 + $num > temp_number1
read number1 < temp_number1


echo $number1

done


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



Sponsored Link
Ads by Google

Response Number 1
Name: jefro
Date: March 21, 2008 at 15:53:56 Pacific
Reply:

Use two numbers, one for each loop.
???

I read it wrong and answer it wrong too. So get off my case you peanut.


0

Response Number 2
Name: nails
Date: March 21, 2008 at 21:49:43 Pacific
Reply:

#!/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



0

Response Number 3
Name: yacob
Date: March 24, 2008 at 09:05:27 Pacific
Reply:

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 $num

done ;


((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.



0

Response Number 4
Name: nails
Date: March 24, 2008 at 18:58:41 Pacific
Reply:

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/bash

num=0
for x in 1 2
do
for i in 1 2 3
do
((num=num+i))
done
done
echo $num

Now, enter the parenthesis and see that the variable resets to 0:


#!/bin/bash

num=0
for x in 1 2
do
(for i in 1 2 3
do
((num=num+i))
done)
done
echo $num

There 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.



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Question on setup faxing ... Would it be possible?



Post Locked

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


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: correct count in the bash script

howto put ctrl chars in bash script www.computing.net/answers/linux/howto-put-ctrl-chars-in-bash-script/20213.html

simple bash script www.computing.net/answers/linux/simple-bash-script/18032.html

Separating commands in bash scripts www.computing.net/answers/linux/separating-commands-in-bash-scripts/20341.html