How to redirect the output within a sub-shell - Unix

This is a discussion on How to redirect the output within a sub-shell - Unix ; Hi For some security reasons, for a particular product line, we are using 'newgrp' utility so that people who are in that group can only can access the product sources. newgrp spawns a new shell. We need to use the ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: How to redirect the output within a sub-shell

  1. How to redirect the output within a sub-shell

    Hi
    For some security reasons, for a particular product line, we are
    using 'newgrp' utility so that people who are in that group can only
    can access the product sources.
    newgrp spawns a new shell.

    We need to use the newgrp utility in our build script. The following
    script works fine.

    #!/bin/ksh
    set -x
    (
    /bin/newgrp nemo < echo "I am here"
    echo "Bye now"
    exit
    endnewgr
    )

    but we need to redirect the output within the newgrp range, which is
    failing.

    #!/bin/ksh
    set -x
    (
    /bin/newgrp nemo < (
    echo "I am here"
    echo "Bye now"
    exit
    ) > output
    endnewgr
    )

    How to solve this.?

    Thanks
    Venkat


  2. Re: How to redirect the output within a sub-shell

    venkat wrote:

    > Hi
    > For some security reasons, for a particular product line, we are
    > using 'newgrp' utility so that people who are in that group can only
    > can access the product sources.
    > newgrp spawns a new shell.
    >
    > We need to use the newgrp utility in our build script. The following
    > script works fine.
    >
    > #!/bin/ksh
    > set -x
    > (
    > /bin/newgrp nemo < > echo "I am here"
    > echo "Bye now"
    > exit
    > endnewgr
    > )
    >
    > but we need to redirect the output within the newgrp range, which is
    > failing.
    >
    > #!/bin/ksh
    > set -x
    > (
    > /bin/newgrp nemo < > (
    > echo "I am here"
    > echo "Bye now"
    > exit
    > ) > output
    > endnewgr
    > )
    >
    > How to solve this.?
    >
    > Thanks
    > Venkat


    One solution:


    #!/bin/ksh
    set -x
    (
    /bin/newgrp nemo < (
    echo "I am here" > output
    echo "Bye now" >> output
    exit
    )
    endnewgr
    )

    The single "carrot" creates a new file, the double "carrots" append to the
    file.

    --



  3. Re: How to redirect the output within a sub-shell

    On 2005-11-04, venkat wrote:
    > Hi
    > For some security reasons, for a particular product line, we are
    > using 'newgrp' utility so that people who are in that group can only
    > can access the product sources.
    > newgrp spawns a new shell.


    Do you really need newgrp? Isn't it enough to set the group
    ownership and permissions appropriately?

    > We need to use the newgrp utility in our build script. The following
    > script works fine.
    >
    > #!/bin/ksh
    > set -x
    > (
    > /bin/newgrp nemo < > echo "I am here"
    > echo "Bye now"
    > exit
    > endnewgr
    > )
    >
    > but we need to redirect the output within the newgrp range, which is
    > failing.


    What does "failing" mean? What actually happens?

    What do you want to happen?

    Is it the stdout or stderr that you want redirected? Or both?

    > #!/bin/ksh
    > set -x
    > (
    > /bin/newgrp nemo <

    newgrp nemo > output <
    > (
    > echo "I am here"
    > echo "Bye now"
    > exit
    > ) > output
    > endnewgr
    > )
    >
    > How to solve this.?


    That depends on what you want to do.

    --
    Chris F.A. Johnson | Author:
    | Shell Scripting Recipes:
    Any code in this post is released | A Problem-Solution Approach,
    under the GNU General Public Licence | 2005, Apress

  4. Re: How to redirect the output within a sub-shell

    Thanks Chris for your reply.

    > Do you really need newgrp? Isn't it enough to set the group
    > ownership and permissions appropriately?


    Yes we do need it. Its not just accessing the files , also executing
    some clear case commands(Clear case is a configuration Management tool
    ), doing builds etc.,

    > What does "failing" mean? What actually happens?
    > What do you want to happen?
    > Is it the stdout or stderr that you want redirected? Or both?


    Ok let me be clear this time. We have a build script of around 400
    lines of codes of code which contains so many functions, clear case
    commands etc., All these things should be executed in newgrp. I am
    trying to execute a sample script, by which the same logic can be
    used for the build script.

    1) I need to execute the script from 'newgrp' which can be done as
    follows.
    /bin/newgrp nemo < set of commands
    endnewgr

    2) I need to redirect the output of each commands, both stdout, stderr
    to a file within newgrp shell. When i try as below,
    /bin/newgrp nemo < (
    echo "I am here"
    echo "Bye now"
    ) > out
    endnewgr

    I am getting output as :
    ====
    svenkat@sparta /home/svenkat/work > sh b.sh
    + /bin/newgrp nemo
    (
    echo "ok"
    echo "bye now"
    ) > out
    Too many ('s.
    ==========

    Otherwords, if
    (
    echo "ok"
    echo "bye now"
    ) > out

    works why not the previous code is failing to redirect the output
    within newgrp subshell?
    or what needs to be done to achieve the same.

    Thanks again
    Venkat


  5. Re: How to redirect the output within a sub-shell


    "venkat" wrote in message news:1131077636.473536.50410@g49g2000cwa.googlegro ups.com...
    : Hi
    : For some security reasons, for a particular product line, we are
    : using 'newgrp' utility so that people who are in that group can only
    : can access the product sources.
    : newgrp spawns a new shell.
    :
    : We need to use the newgrp utility in our build script. The following
    : script works fine.
    :
    : #!/bin/ksh
    : set -x
    : (
    : /bin/newgrp nemo < : echo "I am here"
    : echo "Bye now"
    : exit
    : endnewgr
    : )
    :
    : but we need to redirect the output within the newgrp range, which is
    : failing.
    :
    /bin/newgrp >whatever < ....
    endnewgr


    Or you can even:

    >whatever /bin/newgrp <

    Dan Mercer



  6. Re: How to redirect the output within a sub-shell

    hi venkat,
    what you can do is put all the commands within the ( ) into 1 .sh file
    say for ex. run.sh and then from ( ) execute run.sh (dont miss the
    "." before run.sh) otherwise it will run it in new shell


    #!/bin/ksh
    set -x
    (
    /bin/newgrp nemo < (
    . run.sh > output
    )
    endnewgr
    )

    HTH

    regards
    amey


  7. Re: How to redirect the output within a sub-shell (& newgrp)

    How about some about some useful hints:
    $ cat foo
    #!/bin/ksh
    id -n -g
    [ -f file ] && rm file
    $ cat foo
    #!/bin/ksh
    id -n -g
    [ -f file ] && rm file
    /usr/bin/newgrp cdrom << \__EOT__
    (
    echo "I am here"
    echo "Bye now"
    ) > file
    __EOT__
    ls -g file
    [ -f file ] && rm file
    $ ./foo
    users
    -rw------- 1 cdrom 18 Nov 6 16:19 file
    $ SHELL=/bin/csh ./foo
    users
    Too many ('s.
    ls: file: No such file or directory
    $ 2>>/dev/null fgrep -l "Too many ('s" /bin/*sh* /usr/bin/*sh*
    /bin/bsd-csh
    /bin/csh
    /bin/tcsh
    /usr/bin/tcsh
    $

    Now can you think of the ways the unexpected behavior is likely
    creeping in?

    venky wrote:
    > Thanks Chris for your reply.
    >
    > > Do you really need newgrp? Isn't it enough to set the group
    > > ownership and permissions appropriately?

    >
    > Yes we do need it. Its not just accessing the files , also executing
    > some clear case commands(Clear case is a configuration Management tool
    > ), doing builds etc.,
    >
    > > What does "failing" mean? What actually happens?
    > > What do you want to happen?
    > > Is it the stdout or stderr that you want redirected? Or both?

    >
    > Ok let me be clear this time. We have a build script of around 400
    > lines of codes of code which contains so many functions, clear case
    > commands etc., All these things should be executed in newgrp. I am
    > trying to execute a sample script, by which the same logic can be
    > used for the build script.
    >
    > 1) I need to execute the script from 'newgrp' which can be done as
    > follows.
    > /bin/newgrp nemo < > set of commands
    > endnewgr
    >
    > 2) I need to redirect the output of each commands, both stdout, stderr
    > to a file within newgrp shell. When i try as below,
    > /bin/newgrp nemo < > (
    > echo "I am here"
    > echo "Bye now"
    > ) > out
    > endnewgr
    >
    > I am getting output as :
    > ====
    > svenkat@sparta /home/svenkat/work > sh b.sh
    > + /bin/newgrp nemo
    > (
    > echo "ok"
    > echo "bye now"
    > ) > out
    > Too many ('s.
    > ==========
    >
    > Otherwords, if
    > (
    > echo "ok"
    > echo "bye now"
    > ) > out
    >
    > works why not the previous code is failing to redirect the output
    > within newgrp subshell?
    > or what needs to be done to achieve the same.



  8. Re: How to redirect the output within a sub-shell (& newgrp)

    On 2005-11-07, Michael Paoli wrote:
    > How about some about some useful hints:
    > $ cat foo
    > #!/bin/ksh
    > id -n -g
    > [ -f file ] && rm file
    > $ cat foo
    > #!/bin/ksh
    > id -n -g
    > [ -f file ] && rm file
    > /usr/bin/newgrp cdrom << \__EOT__
    > (
    > echo "I am here"
    > echo "Bye now"
    > ) > file
    > __EOT__
    > ls -g file
    > [ -f file ] && rm file
    > $ ./foo
    > users
    > -rw------- 1 cdrom 18 Nov 6 16:19 file
    > $ SHELL=/bin/csh ./foo
    > users
    > Too many ('s.
    > ls: file: No such file or directory
    > $ 2>>/dev/null fgrep -l "Too many ('s" /bin/*sh* /usr/bin/*sh*
    > /bin/bsd-csh
    > /bin/csh
    > /bin/tcsh
    > /usr/bin/tcsh
    > $
    >
    > Now can you think of the ways the unexpected behavior is likely
    > creeping in?


    Take a good look at the output of grep. What do all those shells
    have in common?

    Which shell is invoked by newgrp? Which shell do you need to run
    your script?

    Why would you use SHELL=/bin/csh when you need to use a
    Bourne-type shell?

    --
    Chris F.A. Johnson | Author:
    | Shell Scripting Recipes:
    Any code in this post is released | A Problem-Solution Approach,
    under the GNU General Public Licence | 2005, Apress

  9. Re: How to redirect the output within a sub-shell

    Hi ameyas

    > /bin/newgrp nemo < > (
    > . run.sh > output
    > )
    > endnewgr
    > )
    >


    The above did't work. i am getting the same error. ie ., Too many '('s

    but this worked.

    thanks guys


  10. Re: How to redirect the output within a sub-shell (& newgrp)

    venkat wrote:
    > Hi ameyas
    > > /bin/newgrp nemo < > > (
    > > . run.sh > output
    > > )
    > > endnewgr
    > > )

    > The above did't work. i am getting the same error. ie ., Too many '('s
    > but this worked.


    But did you ever figure out why you've been getting that specific error
    diagnostic? If not, does the following provide sufficient clues:
    syntax
    C-shell
    newgrp
    Message-ID: <1131323203.410253.150510@g44g2000cwa.googlegroups. com>
    i.e. what shell does newgrp invoke, and how does it make that
    determination.