[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: I think bash logic in a loop like : while [ condition] |read somevar
From: |
Chris Down |
Subject: |
Re: I think bash logic in a loop like : while [ condition] |read somevar is flawed. |
Date: |
Tue, 24 Dec 2013 15:04:46 +0800 |
User-agent: |
Mutt/1.5.22 (2013-10-16) |
On 2013-12-23 23:57:32 +0100, rens wrote:
> Hello,
>
> this script:
> _______________________
> export cval=0
> echo entering while
>
> # listing ten files, to generate some output to count.
> ls -1 /usr/bin|head -10 |while read fname
Just use a `for` loop and a glob. Really. It's that easy.
> do
> cval=$(( cval +1 ))
> echo cval = $cval file = $fname
> done
> # one would think cval is now 10. but it is not, contrary to any other
> programming language........
Really, all other programming languages propagate variables from
children to parents? I find that hard to believe.
> that makes no sense.
Yes, it does. You created a subshell. How do you expect for the variable
to propagate back to the parent, where you are expanding it?
> Please Don't bother to tell me it s the way you guys think it should
> technically work.
> I know you think something along those lines, at least thats what I am
> told....
Don't use a subshell if you don't want one.
> However, no one in this world having to solve real life issues with
> software, is interested in how it technically works. we need real life
> logic and software that can deal with real life challenges and requirements.
Then stop using a subshell.
> Bash should not work that way. no programming language handles logic this
> way. Not pascal, korn shell, c shell, cobol, c, c++, lua , fortran or any
> other language i ever used.
None of the languages you listed propagate variables from children to
parents directly. I don't know why you think you should have that
variable in the parent when you populated it in a subshell...
pgpSDy_S8dctM.pgp
Description: PGP signature
Re: I think bash logic in a loop like : while [ condition] |read somevar is flawed., Pierre Gaston, 2013/12/24
Re: I think bash logic in a loop like : while [ condition] |read somevar is flawed., Greg Wooledge, 2013/12/24