Re: Executing a command in sub-shell

From: Kevin Rodgers (ihs_4664_at_yahoo.com)
Date: 02/12/04


Date: Wed, 11 Feb 2004 17:40:59 -0700

udayan wrote:
> Kevin Rodgers <ihs_4664@yahoo.com> wrote in message
news:<40292020.7050703@yahoo.com>...
> Now from the code that you told me -
> ...
>>>>cleartool setview view_name <<- _setview
>>>> command_v_1
>>>> ...
>>>> command_v_N
>>>> cleartool setact activity_name <<- _setact
>>>>
> ...
> After this point i should be into the command prompt and not execute
> the code further (till i type exit from the command prompt).

Well, that's tricky. If you don't redirect standard input for the
`cleartool setact activity_name` command, it inherits its input from the
`cleartool setview view_name` command, whose standard input is the here
document terminated by _setview.

Unless you don't have any canned commands that have to run in the
`cleartool setact activity_name` subshell...

> Only when i type exit from the command prompt then i should go into
> the sub-shell created by "cleartool setview view_name".

... Now I understand what you want to do.

Onew way would might be to redirect the second subshell's input from the
terminal, either /dev/tty or `tty` (instead of a here document):

        cleartool setact activity_name << /dev/tty

A better way might be like this:

{
   echo command_v_1
   ...
   echo command_v_N
   cleartool setact activity_name
} |
cleartool setview view_name

> Apart from above mentioned, I am not able to understand the behaviour
> mentioned below -
>
> ###########Code -
> #!/bin/sh
>
> MY_NAME="UDAYAN"
> cleartool setview usingh_dev_view <<- _setview
> echo "my view is usingh_dev_view "
> cleartool pwv
> cleartool setact Tools <<- _setact
> val1=0
> echo "val1 = ${val1}"
> echo ${MY_NAME}
> _setact
> cleartool pwv
> _setview
> echo "val1 = ${val1}"
>
> #############script ends here . Now the output follows
> my view is usingh_dev_view
> Working directory view: ** NONE **
> Set view: usingh_dev_view
> Set activity "Tools" in view "usingh_dev_view".
> Working directory view: ** NONE **
> Set view: usingh_dev_view
> val1 =
>
>
> ########### here the value of val1 is not printed ?? why??

It's not clear at all, because there are 2 `echo val1` commands in your
script and only 1 line of output, and apparently the nested subshells'
eoutput is interleaved (perhaps because some commands are writing to
standard output and some to standard error).

In any case, the first reference to ${val1} occurs within the here
document, which is expanded by the shell where it has not been set. To
expand it dynamically in the subshell, it needs to be quoted in the here
document:

        echo "val1 = \${val1}"

(And since it's 2 levels deep, it may need to be quoted twice. :-)

The second occurrence occurs in the shell script, after the subshell
where it was set has exited. Just as a child process can't effect the
environment of its parent process, a subshell can't set a variable in
its parent shell, so you're out of luck there.

-- 
Kevin Rodgers


Relevant Pages

  • Re: safe scanf( ) or gets
    ... programs which read from standard input and write to ... You don't know about operating systems providing command line ... filter programs. ... on a typical *nix system without using stream filters, e.g. grep, ...
    (comp.lang.c)
  • Re: Connecting Fortran code to TCP/IP socket
    ... server via streaming TCP/IP socket connection. ... If you invoke your program with the netcat command the standard input ...
    (comp.lang.fortran)
  • Re: How do I give 2 parameters to programs in an unix enviroment?
    ... named pipe in /tmp, execute the commands contained in the parenthesis ... in a subshell, and connect the stdout of the subshell into that named ... So it's sort of like using temp files, ... subshell to finish before running the main command, ...
    (freebsd-questions)
  • Re: Defining Variables
    ... It will be visible with zsh, and some implementations of ksh, it ... ksh93) the last pipe command was not executed in a subshell. ... make any assumption either way in a POSIX script. ...
    (comp.unix.shell)
  • Re: Using cmd-line pipe with and without xargs
    ... 'prog | anotherprog' puts the standard output of prog into ... the standard input of anotherprog." ... The OS supplies the command line arguments in an analogous way. ... what I did was neglect to read stdin from my ...
    (comp.unix.shell)