[Top][All Lists]

[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: Pierre Gaston
Subject: Re: I think bash logic in a loop like : while [ condition] |read somevar is flawed.
Date: Tue, 24 Dec 2013 09:13:23 +0200

On Tue, Dec 24, 2013 at 12:57 AM, rens <address@hidden> wrote:

> Subject: [50 character or so descriptive subject here (for reference)]
> Configuration Information [Automatically generated, do not change]:
> Machine: x86_64
> OS: linux-gnu
> Compiler: gcc -I/home/abuild/rpmbuild/BUILD/bash-4.2
> -L/home/abuild/rpmbuild/BUILD/bash-4.2/../readline-6.2
> Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
> -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-suse-linux-gnu'
> -DCONF_VENDOR='suse' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash'
> -DSHELL -DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib   -fmessage-length=0
> -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables
> -fasynchronous-unwind-tables -g -D_GNU_SOURCE -DRECYCLES_PIDS -Wall -g
> -std=gnu89 -Wuninitialized -Wextra -Wno-unprototyped-calls -Wno-switch-enum
> -Wno-unused-variable -Wno-unused-parameter -ftree-loop-linear -pipe
> -fprofile-use
> uname output: Linux dep2.fritz.box 3.7.10-1.16-default #1 SMP Fri May 31
> 20:21:23 UTC 2013 (97c14ba) x86_64 x86_64 x86_64 GNU/Linux
> Machine Type: x86_64-suse-linux-gnu
> Bash Version: 4.2
> Patch Level: 42
> Release Status: release
>  address@hidden
> Description:
>     [Detailed description of the problem, suggestion, or complaint.]
> 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
> 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........
> echo  " after while: cval = $cval"
> _______________________________
> does not set the value of cval after exiting the while loop.
> that makes no sense.
> 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....
> 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.
> 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.

Actually only ksh93 doesn't work this way, other "ksh" variants work like
In newer bash you can do, if job control is disable (that is in a script):
shopt -s lastpipe.

Note that even with this, only the right hand side doesn't run in a forked
subshell, eg in ksh93 or bash with last pipe:
var=foo | true;echo $var # prints nothing

At least one side of the pipe has to be in a child process.
The same thing is true if you use a pipe and fork a process in these other
languages you mention.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]