[comp.unix.shell] bourne sh, problems with var setting in case/esac

bothe@net7.uucp (11/15/90)

in sh, case does not save vars correct (i think)

such a script shtest:
#==== start script shtest =====
echo "1=$1, 2=$2, #=$#"
case $1 in
-d)
	shift
	echo "1=$1, 2=$2, #=$#"
	b="$1 x"
	echo ++++++++++++++++ $b
esac |
cat
echo ++++++++++++++++ $b
echo "1=$1, 2=$2, #=$#"
#==== end script shtest =====

called with:
shtest -d affe

results:                       |    remarks:
_________________________      |    _____________________
1=-d, 2=affe, #=2              |    orig params, ok
1=affe, 2=, #=1                |    ok, params shifted
++++++++++++++++ affe x        |    ok
++++++++++++++++               |    oops, where is content of b ?
1=-d, 2=affe, #=2              |    hey, hu, what is going on with shift ?

i did expect only 1 parameter ($1=affe) after esac, and var b set as
in case !

is it a bug or a feature

if i don't pipe esac and not use cat, all is ok, but i want to pipe !


what to do ?
S I E M E N S   Richard Bothe, NIXDORF COMPUTER AG, Dep PDT-M 24
-------------   Berliner Str. 95, W-8000 Munic (West Germany)
N I X D O R F   Tel.: +49 89 3601 2956, USA: bothe.muc@nixdorf.com
    (SNI)       !USA: bothe.muc@nixdorf.de, NERV: bothe.muc

cpcahil@virtech.uucp (Conor P. Cahill) (11/15/90)

In article <1236@netmuc.UUCP> bothe@net7.uucp writes:
>#==== start script shtest =====
>case $1 in
>esac |
>cat
>echo ++++++++++++++++ $b

The problem is any processing run in a sub-shell will not effect the 
variables in the parent shell.  Because you use a pipe, the shell
must run a sub-shell to handle the case portion of the pipeline.

>if i don't pipe esac and not use cat, all is ok, but i want to pipe !

Work out some other mechanism to get your desired results.

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

news@usenet.ins.cwru.edu (11/15/90)

In article <1236@netmuc.UUCP> bothe@net7.uucp writes:

>if i don't pipe esac and not use cat, all is ok, but i want to pipe !

In the immortal words of Guy Harris (or is that the words of the immortal
Guy Harris? :-):

	``You've been zapped by Mr. Subshell''

Sh runs control structures with pipes in a subshell.  Variables set in a
subshell cannot affect the parent's settings.

Chet
-- 
Chet Ramey				``I die, Horatio''
Network Services Group, Case Western Reserve University
chet@ins.CWRU.Edu
                My opinions are just those, and mine alone.

rdavis@connie.UUCP (Ray Davis) (11/16/90)

In <1990Nov15.155604.16714@usenet.ins.cwru.edu> news@usenet.ins.cwru.edu writes:

>>if i don't pipe esac and not use cat, all is ok, but i want to pipe !

>Sh runs control structures with pipes in a subshell.  Variables set in a
>subshell cannot affect the parent's settings.

It isn't the control structure that is the problem, it's the
pipe.  Note that in ksh (and maybe SVR? sh), the pipe causes a
subshell, but redirecting the output of the case to a file does
not.  Note also that if you 'cat | case', the case is not in a
subshell.

Ray
---
Ray Davis
Convex Computer GmbH, Frankfurt, West Germany
unido!connie!rdavis, uunet!convex!rdavis, rdavis@convex.com, +49-69-666-8081

andyc@bucky.intel.com (Andy Crump) (11/30/90)

>>>>> On 15 Nov 90 19:05:53 GMT, rdavis@connie.UUCP (Ray Davis) said:

Ray> In <1990Nov15.155604.16714@usenet.ins.cwru.edu> news@usenet.ins.cwru.edu writes:

>>if i don't pipe esac and not use cat, all is ok, but i want to pipe !

>Sh runs control structures with pipes in a subshell.  Variables set in a
>subshell cannot affect the parent's settings.

Ray> It isn't the control structure that is the problem, it's the
Ray> pipe.  Note that in ksh (and maybe SVR? sh), the pipe causes a
Ray> subshell, but redirecting the output of the case to a file does
Ray> not.  Note also that if you 'cat | case', the case is not in a
Ray> subshell.

I tried this on Intel SVR4 version 2.0 with both ksh and sh and they
behave exactly as everyone describes.  The pipe causes a subshell.
You have been zapped by Mr. SubShell.
--

    -- Andy Crump

    ...!tektronix!reed!littlei!andyc | andyc@littlei.intel.com
    ...!uunet!littlei!andyc          | andyc@littlei.uu.net

Disclaimer: Any opinions expressed here are my own and 
            not representive of Intel Corportation.