Thread View: comp.unix.shell
5 messages
5 total messages
Started by bothe
Wed, 14 Nov 1990 19:43
bourne sh, problems with var setting in case/esac
Author: bothe
Date: Wed, 14 Nov 1990 19:43
Date: Wed, 14 Nov 1990 19:43
42 lines
1611 bytes
1611 bytes
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
Re: bourne sh, problems with var setting in case/esac
Author: Conor P. Cahill
Date: Thu, 15 Nov 1990 13:35
Date: Thu, 15 Nov 1990 13:35
19 lines
1226 bytes
1226 bytes
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
Re: bourne sh, problems with var setting in case/esac
Author: news
Date: Thu, 15 Nov 1990 15:56
Date: Thu, 15 Nov 1990 15:56
18 lines
1069 bytes
1069 bytes
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.
Re: bourne sh, problems with var setting in case/esac
Author: Ray Davis
Date: Thu, 15 Nov 1990 19:05
Date: Thu, 15 Nov 1990 19:05
18 lines
1221 bytes
1221 bytes
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
Re: bourne sh, problems with var setting in case/esac
Author: Andy Crump
Date: Thu, 29 Nov 1990 16:02
Date: Thu, 29 Nov 1990 16:02
27 lines
1708 bytes
1708 bytes
>>>>> 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.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads