LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getting a variable out of a WHILE loop. (https://www.linuxquestions.org/questions/programming-9/getting-a-variable-out-of-a-while-loop-290623/)

fhinkle 02-15-2005 04:10 PM

getting a variable out of a WHILE loop.
 
Ok, my problem is this:

I want to have $count = the # of file written by the loop. Adding 3 every time the loop is run. At the end of the script I want to be able to echo the total # of files written.

In what i have, the value for $count increases as it should until the end of the looop, where the value of $count returns to 0

count=0

#there is more code in here in my script, I just pasted the relevant part.

cat 10tapes | while read tapename
do
dd bs=64k if=/dev/nst0 of=$tapename.header.ebcdic
echo "Header Complete"
dd bs=64k if=/dev/nst0 of=$tapename.data.ebcdic
echo "Data Complete"
dd bs=64k if=/dev/nst0 of=$tapename.trailer.ebcdic
echo "Trailer Complete"
chmod -w $tapename*
ls -l $tapename*
echo Rewinding Tape
mt -f /dev/nst0 rewind
echo Ejecting Tape
mt -f /dev/nst0 eject
echo Finished with tape $tapename
(( count=$count+3 ))
echo "******* DEBUG COUNT $count *********"
done

echo "******* DEBUG COUNT $count after done *********"
# The first DEBUG COUNT line gives the correct value.
# The second DEBUG COUNT gives the value of $count as 0
# I think The While loop is eating my variable, and I don't know how to get it out. Perhaps this question belongs in the newbie forum?

LasseW 02-15-2005 05:08 PM

Use this while construct instead:

while read tapename
do
...
done <10tapes

dustu76 02-16-2005 01:30 AM

Code:

SF1B : /supmis/soumen/tmp > cat 3tapes
tape1
tape2
tape3
SF1B : /supmis/soumen/tmp > cat tpp
cat 3tapes |while read tapename
do
echo "processing $tapename"
(( count=$count+3 ))
done
echo $count
SF1B : /supmis/soumen/tmp > bash tpp
processing tape1
processing tape2
processing tape3

SF1B : /supmis/soumen/tmp > sh tpp
processing tape1
processing tape2
processing tape3

SF1B : /supmis/soumen/tmp > ksh tpp
processing tape1
processing tape2
processing tape3
9
SF1B : /supmis/soumen/tmp >

Use something like expr etc. for addition... there are too many ways to add ;)

HTH.

fhinkle 02-16-2005 08:48 AM

Quote:

Originally posted by LasseW
Use this while construct instead:

while read tapename
do
...
done <10tapes

Ok, this worked.... odd... I don't suppose you could explain Why it worked? :confused: Or, more importantly what was happening to my variable the way i was doing it?

I remember i started out trying to use a while loop like this one, but I had a different problem which lead to me using cat. Now i feel silly :)


Thanks alot for your help! :)


And Dustu76, I must admit i had no idea what was happening in the code you posted. I am still new to this whole thing. Trying to get as much practice in as I can though. Need to really get it all in my head :)

In regard to using expr for addition. I'm not sure why, I just think the double parentheses look nicer. Does it somehow work better to use expr? or is it just a personal preference thing.

keefaz 02-16-2005 09:00 AM

You could use let too like :
count=1
let "count+=3"
echo $count

theYinYeti 02-16-2005 09:13 AM

No.

LasseW is right. That's because when you use pipes, each side of the pipe is ran as a subshell. And there's (almost) no way a subshell can export a value to the parent shell.
If you want to run
Code:

B | A
, and still have A in the current shell, the general solution is to do
Code:

A < <(B)
instead. Of course, if B is cat "b", this can be replaced by
Code:

A <"b"
and if B is echo "b", this can be replaced by
Code:

A <<<"b"
Yves.

dustu76 02-16-2005 11:58 PM

If theYinYeti is right, then in my earlier post, how come the ksh is aware of the increments & reports then correctly? Seems like the answer also depends on the shell you are using.

As far as using expr, let, (()) etc., you can choose anything that suits you. I find let & (()) faster than expr 'cause they are builtins. However (()) & let don't work with my version of sh:

Code:

SF1B : /supmis/soumen/tmp > cat ttp
val=20
val1=`expr $val + 20`
(( val2=$val+20 ))
let "val3=$val+20"
echo "val1=$val1 val2=$val2 val3=$val3"
SF1B : /supmis/soumen/tmp >
SF1B : /supmis/soumen/tmp > sh ttp
ttp: let: not found
val1=40 val2= val3=
SF1B : /supmis/soumen/tmp >
SF1B : /supmis/soumen/tmp > ksh ttp
val1=40 val2=40 val3=40
SF1B : /supmis/soumen/tmp >

HTH.

bigearsbilly 02-22-2005 05:37 AM

Quote:

If theYinYeti is right, then in my earlier post, how come the ksh is aware of the increments & reports then correctly? Seems like the answer also depends on the shell you are using.
true.

eg:
Code:

billym.primadtpdev>cat ~/1

echo hello | read x
echo x=$x

billym.primadtpdev>ksh ~/1
x=hello
billym.primadtpdev>bash ~/1
x=
billym.primadtpdev>

another reason I prefer ksh ;)

dustu76 02-22-2005 05:43 AM

I second that billy :)


All times are GMT -5. The time now is 03:52 AM.