ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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.
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.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.