tcsh - to subshell or not to subshell

tcsh - to subshell or not to subshell

Post by Carolynn German » Thu, 10 Oct 2002 08:11:25



I have a requirement to able to set environment variables in s script
and provide the value remain in the invoking shell.

For example using ksh I can create a script a.sh as:
      CVS=/export/home1/cvs_directory
      export CVS
Then I can execute the script in the same shell  (using the ".
<sp><file> )as follows:
     .  ./a.sh

This results in the envir var CVS being set in the invoking shell.

Now, I have users wanting to use tcsh; can this be done in tcsh??
ie invoke a script that runs in the invoking shell so that environment
changes are retained ??

Thanks in advance for you help.

Don

 
 
 

tcsh - to subshell or not to subshell

Post by Charles Dem » Thu, 10 Oct 2002 09:13:44




Quote:>I have a requirement to able to set environment variables in s script
>and provide the value remain in the invoking shell.

>For example using ksh I can create a script a.sh as:
>      CVS=/export/home1/cvs_directory
>      export CVS
>Then I can execute the script in the same shell  (using the ".
><sp><file> )as follows:
>     .  ./a.sh

>This results in the envir var CVS being set in the invoking shell.

>Now, I have users wanting to use tcsh; can this be done in tcsh??
>ie invoke a script that runs in the invoking shell so that environment
>changes are retained ??

>Thanks in advance for you help.

>Don

In tcsh the equivalent to the . command is the source command

man csh
man tcsh

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.


 
 
 

tcsh - to subshell or not to subshell

Post by Bruce Barnet » Thu, 10 Oct 2002 10:28:31



Quote:> In tcsh the equivalent to the . command is the source command

Or you can execute a ksh script that sets the environment, which then
executes csh again. If you want to return to your previous
environment, type "exit." If you want the new environment to replace
the old one, do "exec kshscript" which does "exec csh" and you are set.

--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

 
 
 

tcsh - to subshell or not to subshell

Post by CornĂ© Beers » Thu, 10 Oct 2002 21:35:23



> I have a requirement to able to set environment variables in s script
> and provide the value remain in the invoking shell.

> For example using ksh I can create a script a.sh as:
>       CVS=/export/home1/cvs_directory
>       export CVS

The csh (and tcsh) equivalent:

setenv CVS /export/home1/cvs_directory

Quote:> Then I can execute the script in the same shell  (using the ".
> <sp><file> )as follows:
>      .  ./a.sh

the csh (and tcsh) equivalent:

source ./a.csh

(detail: the sourced file must have csh syntax!!!)

Quote:

> This results in the envir var CVS being set in the invoking shell.

> Now, I have users wanting to use tcsh; can this be done in tcsh??
> ie invoke a script that runs in the invoking shell so that environment
> changes are retained ??

> Thanks in advance for you help.

> Don

 
 
 

tcsh - to subshell or not to subshell

Post by Juergen Hec » Fri, 15 Nov 2002 23:58:54



> I have a requirement to able to set environment variables in s script
> and provide the value remain in the invoking shell.

> For example using ksh I can create a script a.sh as:
>       CVS=/export/home1/cvs_directory
>       export CVS
> Then I can execute the script in the same shell  (using the ".
> <sp><file> )as follows:
>      .  ./a.sh

> This results in the envir var CVS being set in the invoking shell.

> Now, I have users wanting to use tcsh; can this be done in tcsh??
> ie invoke a script that runs in the invoking shell so that environment
> changes are retained ??

> Thanks in advance for you help.

> Don

Basic Idea

$ cat a.sh
#! /bin/ksh
[ "$i" != dummy ] && { i=dummy ; exec $0 ; }
CVS=/export/home1/cvs_directory  export CVS

unset i
exec /bin/tcsh
## END

Start the script:  ./a.sh
NOT:  . ./a.sh

Regards
Juergen

 
 
 

tcsh - to subshell or not to subshell

Post by Keith Thompso » Sat, 08 Feb 2003 08:40:33




> > I have a requirement to able to set environment variables in s script
> > and provide the value remain in the invoking shell.

> > For example using ksh I can create a script a.sh as:
> >       CVS=/export/home1/cvs_directory
> >       export CVS
> > Then I can execute the script in the same shell  (using the ".
> > <sp><file> )as follows:
> >      .  ./a.sh

> > This results in the envir var CVS being set in the invoking shell.

> > Now, I have users wanting to use tcsh; can this be done in tcsh??
> > ie invoke a script that runs in the invoking shell so that environment
> > changes are retained ??

> > Thanks in advance for you help.

> > Don

> Basic Idea

> $ cat a.sh
> #! /bin/ksh
> [ "$i" != dummy ] && { i=dummy ; exec $0 ; }
> CVS=/export/home1/cvs_directory  export CVS

> unset i
> exec /bin/tcsh
> ## END

> Start the script:  ./a.sh
> NOT:  . ./a.sh

Um, no.  That will invoke a new tcsh process; the point is to set
environment variables in an existing tcsh process.

For the current purposes, you can consider csh and tcsh to be
equivalent; likewise sh, ksh, and bash are equivalent.  (There are
differences, of course, but I'll avoid anything that depends on them.)

The csh equivalent of sh's "." is "source".  For example, given a file
"set-cvs.csh" containing the line:

    setenv CVS /export/home1/cvs_directory

a user can type "source set-cvs.csh" to set the $CVS environment
variable in his current shell.  (Note that the "source" command,
unlike ".", doesn't use $PATH.)

Another approach is to have a command that prints out a series of
commands; the user can then "eval" the result.  For example, given
the following command, called "set-cvs":

    #!/bin/sh

    echo "setenv CVS /export/home1/cvs_directory"

a csh user can type

    eval `set-cvs`

One advantage of this is that the command can be written in any
language (I'd probably use Perl for anything complex), and can
generate commands for either sh or csh depending on a command-line
option or the value of $SHELL.  A disadvantage is that it's a little
more complicated for the user, but this can be hidden in an alias or
shell function.

Note that csh's backticks join the output into a single line, so
you'll probably need to add semicolons to your generated commands if
there's more than one.  sh's backticks do the same thing unless
they're enclosed in double quotes.

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"

 
 
 

tcsh - to subshell or not to subshell

Post by Tim Cargi » Sat, 08 Feb 2003 18:47:46





> > > I have a requirement to able to set environment variables in s script
> > > and provide the value remain in the invoking shell.

[ ... ]
> Another approach is to have a command that prints out a series of
> commands; the user can then "eval" the result.  For example, given
> the following command, called "set-cvs":

>     #!/bin/sh

>     echo "setenv CVS /export/home1/cvs_directory"

> a csh user can type

>     eval `set-cvs`

> One advantage of this is that the command can be written in any
> language (I'd probably use Perl for anything complex), and can
> generate commands for either sh or csh depending on a command-line
> option or the value of $SHELL.  A disadvantage is that it's a little
> more complicated for the user, but this can be hidden in an alias or
> shell function.

> Note that csh's backticks join the output into a single line, so
> you'll probably need to add semicolons to your generated commands if
> there's more than one.  sh's backticks do the same thing unless
> they're enclosed in double quotes.

Yes ...

I don't know how this thread got re-started from last year,
but it got me thinking.  One problem with environment-setting
for multi-shells is having to maintain two files - one for [t]csh
users and one for (most) all others.  But if the Bourne family
users had a 'setenv' function declared by, ideally, '.' executing
a common function library 'dot' file, then they could use
the [t]csh environment-setting 'source' file - providing it
contained only 'setenv' statements.

set-env.csh (contains only setenv statements)

...
setenv CVS /usr/local/cvs
...

common_funcs.sh (Bourne/korn/zsh common function library file)

setenv() {
  eval ${1}=${2}
  export $1

Quote:}

t1 (sh/ksh/bash/zsh executable file)

...

. common_funcs.sh
. set-env.csh

...

We Must Be Really Bored,

Tim

 
 
 

1. `Nested (`Nested Subshells`) Subshells`

I'm trying to nest commands in a script file so that I can do an if
checking in one command.  Here's the command thus far:

#!/bin/csh
if ((`/bin/hostname | fgrep -c mirror` == 1) && `(((finger | grep
"whoami") | grep "tty | cut -c9-11") | fgrep -c vortex)` != 0) then
...
endif

As can be seen, even with the parentheses, the shell doesn't like it.
It won't ever run the inside when it is supposed to.  The problem seems
to be that when it fingers, it greps for the string "whoami", not for
the string returned if one ran "whoami".  But since the finger is itself
enclosed in backquotes, how does one quote the whoami such that it
executes?

Please E-mail or CC all responses to facilitate things, or I might miss them.

---===<<<(((###     -=Todd Gleason=-     -=Cool-Man=-     ###)))>>>===---
From computer arguments to the love of women, as far back as the dawn of
Cal-Clue-Less to the future of balloons, with music arcane and actions
forbade, for the Command Chair and the glory of Ramen, this is ME, and
these are the times upon which the greatest of sagas shall be written. . . .

2. depmod & modules

3. aliases in subshells with tcsh

4. pppd-problem

5. Odd tcsh behaviour: subshells & stdout

6. web site that list kernel changes

7. invoking a script (not in a subshell)

8. soucre of unix commands

9. gnu midnight commander bash subshell not working (aix 4.2.1)

10. invoking a script (not in a subshell)

11. posix shell and builtin read; subshell or not?

12. exiting out of subshell but not whole script

13. trap not work in program with a subshell environment