LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2005, 03:10 PM   #1
fhinkle
LQ Newbie
 
Registered: Feb 2005
Location: Waldorf, MD
Distribution: Red Hat Enterprise/SuSe Linux
Posts: 6

Rep: Reputation: 0
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?
 
Old 02-15-2005, 04:08 PM   #2
LasseW
Member
 
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108

Rep: Reputation: 15
Use this while construct instead:

while read tapename
do
...
done <10tapes
 
Old 02-16-2005, 12:30 AM   #3
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
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.
 
Old 02-16-2005, 07:48 AM   #4
fhinkle
LQ Newbie
 
Registered: Feb 2005
Location: Waldorf, MD
Distribution: Red Hat Enterprise/SuSe Linux
Posts: 6

Original Poster
Rep: Reputation: 0
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? 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.
 
Old 02-16-2005, 08:00 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
You could use let too like :
count=1
let "count+=3"
echo $count
 
Old 02-16-2005, 08:13 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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.

Last edited by theYinYeti; 02-22-2005 at 07:54 AM.
 
Old 02-16-2005, 10:58 PM   #7
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
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.
 
Old 02-22-2005, 04:37 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,487

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
Old 02-22-2005, 04:43 AM   #9
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
I second that billy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell variable in Linux is reset after while loop CBenner Programming 7 05-19-2006 09:46 AM
incrementing variable in an until loop jeffreybluml Programming 2 05-12-2005 07:45 AM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
calling a variable within a variable sdandeker Programming 9 04-28-2004 03:55 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration