Re: ksh (IEEE Std 1003.1-2008 ?)

2010-01-09 Thread Philip Guenther
On Fri, Jan 8, 2010 at 10:18 PM, J.C. Roberts list-...@designtools.org wrote:
 I've been looking for standards on correct variable scope in ksh but
 the IEEE Std 1003.1-2008 stuff on the Open Group site doesn't mention
 any rules?
 http://www.opengroup.org/onlinepubs/9699919799/utilities/contents.html

The key section is 2.12:

http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_12

To selectively quote:

A shell execution environment consists of the following:
...
*  Shell parameters that are set by variable assignment (see the set special
built-in) or from the System Interfaces volume of POSIX.1-2008
environment
inherited by the shell when it begins (see the export special built-in)
...
A subshell environment shall be created as a duplicate of the shell environment,
except that signal traps set by that shell environment shall be set to
the default
values. Changes made to the subshell environment shall not affect the shell
environment.
...
Additionally, each command of a multi-command pipeline is in a subshell
environment; as an extension, however, any or all commands in a pipeline may
be executed in the current environment. All other commands shall be executed
in the current shell environment.



 I found an interesting behavior in our ksh (pdksh) implementation, and
 I'd like to know if the behavior is considered correct?

Yes, it is.


 When you append to a variable within a 'for' loop, the changes are
 exist after the loop ends, but if you do the same within a 'while'
 loop, the changes are lost?

It has nothing to do with 'for' vs 'while' and everything to do with your using
'for' as a single-command and 'while' in a multi-command pipeline.  If
you prefix
the 'for' command with echo foo | then you'll see the same behavior with it.


 I realize the IEEE/POSIX standard was based on ksh93, and our pdksh is
 only partially compliant with ksh93, but I'm wondering if the correct
 behavior for variable scope is formally standardized and/or documented
 anywhere?

See above.


Philip Guenther



Re: ksh (IEEE Std 1003.1-2008 ?)

2010-01-09 Thread J.C. Roberts
On Sat, 9 Jan 2010 08:51:31 +0100 Matthias Kilian
k...@outback.escape.de wrote:

 n Fri, Jan 08, 2010 at 10:18:39PM -0800, J.C. Roberts wrote:
  When you append to a variable within a 'for' loop, the changes are
  exist after the loop ends, but if you do the same within a 'while'
  loop, the changes are lost?
 [...]
  # Now we try the same type of thing with the 'while' loop.
  cat list.txt | while read -r WNAME; do
 [...]
 
 That while loop is running in a separate process; the parent process
 won't see any variable changes made in the loop.
 
 Ciao,
   Kili


Bah! I totally missed that. Makes sense now. Thanks Kili.

If I use redirection rather than piping for input to the `while` loop,
it is not a separate process, and works like the 'for' loop.


while read -r WNAME; do
  if [[ -z $WLIST ]]; then
WLIST=$WNAME;
  else
WLIST=$WLIST $WNAME;
  fi
  printf WLIST: %s\n $WLIST;
done  list.txt


If I wanted to use the other method with cat list.txt |, I'd have to
use a co-process | to talk to the 'while' loop (and read -p/print
-p) and get the changes to the variables.

-- 
J.C. Roberts



Re: ksh (IEEE Std 1003.1-2008 ?)

2010-01-09 Thread Matthew Szudzik
On Sat, Jan 09, 2010 at 12:43:05AM -0800, J.C. Roberts wrote:
 If I use redirection rather than piping for input to the `while` loop,
 it is not a separate process, and works like the 'for' loop.

I have seen some Unix systems where this doesn't work, either.  See

 
http://groups.google.com/group/comp.unix.programmer/browse_thread/thread/20872c6fe79dd153/f3a4f184d0c6a718



Re: ksh (IEEE Std 1003.1-2008 ?)

2010-01-09 Thread J.C. Roberts
On Sat, 9 Jan 2010 14:56:02 + Matthew Szudzik
mszud...@andrew.cmu.edu wrote:

 On Sat, Jan 09, 2010 at 12:43:05AM -0800, J.C. Roberts wrote:
  If I use redirection rather than piping for input to the `while`
  loop, it is not a separate process, and works like the 'for' loop.
 
 I have seen some Unix systems where this doesn't work, either.  See
 
  
 http://groups.google.com/group/comp.unix.programmer/browse_thread/thread/20872c6fe79dd153/f3a4f184d0c6a718

Reminds me of this:

It is easier to port a shell than a shell script.
 -- Larry Wall

:-)


-- 
J.C. Roberts



Re: ksh (IEEE Std 1003.1-2008 ?)

2010-01-08 Thread Matthias Kilian
On Fri, Jan 08, 2010 at 10:18:39PM -0800, J.C. Roberts wrote:
 When you append to a variable within a 'for' loop, the changes are
 exist after the loop ends, but if you do the same within a 'while'
 loop, the changes are lost?
[...]
 # Now we try the same type of thing with the 'while' loop.
 cat list.txt | while read -r WNAME; do
[...]

That while loop is running in a separate process; the parent process
won't see any variable changes made in the loop.

Ciao,
Kili