Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 13 Feb 1996 14:31:34 -0800 (PST)
From:      Archie Cobbs <archie@tribe.com>
To:        freebsd-questions@freebsd.org
Subject:   stupid sh tricks
Message-ID:  <199602132231.OAA10024@bubba.tribe.com>

next in thread | raw e-mail | index | archive | help

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199602132231.OAA10024>