From owner-freebsd-questions Tue Feb 13 14:32:14 1996 Return-Path: owner-questions Received: (from root@localhost) by freefall.freebsd.org (8.7.3/8.7.3) id OAA13401 for questions-outgoing; Tue, 13 Feb 1996 14:32:14 -0800 (PST) Received: from bubba.tribe.com ([205.184.207.7]) by freefall.freebsd.org (8.7.3/8.7.3) with SMTP id OAA13396 for ; Tue, 13 Feb 1996 14:32:08 -0800 (PST) Received: (from archie@localhost) by bubba.tribe.com (8.6.12/8.6.12) id OAA10024 for freebsd-questions@freebsd.org; Tue, 13 Feb 1996 14:31:34 -0800 From: Archie Cobbs Message-Id: <199602132231.OAA10024@bubba.tribe.com> Subject: stupid sh tricks To: freebsd-questions@freebsd.org Date: Tue, 13 Feb 1996 14:31:34 -0800 (PST) X-Mailer: ELM [version 2.4 PL24] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-questions@freebsd.org Precedence: bulk Hi, This is a pretty basic sh(1) question. I apologize that it's not FreeBSD specific... but I couldn't seem to find the answer in the man page or ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/comp/unix/shell. Also, I don't get news right now... ok, enough apologies... Consider the following script: #!/bin/sh foo () { GLOBAL_ONE="foo_one" GLOBAL_TWO="foo_two" while read ARG; do GLOBAL_TWO=${ARG} done return 0 } bar () { GLOBAL_ONE="bar_one" GLOBAL_TWO="bar_two" } echo before : GLOBAL_ONE=${GLOBAL_ONE} and GLOBAL_TWO=${GLOBAL_TWO} echo "value" | foo echo foo set: GLOBAL_ONE=${GLOBAL_ONE} and GLOBAL_TWO=${GLOBAL_TWO} bar echo bar set: GLOBAL_ONE=${GLOBAL_ONE} and GLOBAL_TWO=${GLOBAL_TWO} The output of this script is: before : GLOBAL_ONE= and GLOBAL_TWO= foo set: GLOBAL_ONE= and GLOBAL_TWO= bar set: GLOBAL_ONE=bar_one and GLOBAL_TWO=bar_two Why!? Seems like have a "read" in the function foo() changes variable scoping or something. This "read" seems to be a very broken command... another example: $ cat foo foo-contents $ read VAR < foo $ echo $VAR foo-contents $ VAR= $ echo $VAR $ cat foo | read VAR $ echo $VAR $ exit So "read" seems to "know" whether its input is coming from a file or a pipe. Is this a bug or a feature? :-) Thanks, -Archie _______________________________________________________________________________ Archie L. Cobbs, archie@tribe.com * Tribe Computer Works http://www.tribe.com