Newbie: tcsh/csh 'source' command question

Newbie: tcsh/csh 'source' command question

Post by Ada » Sat, 17 May 2003 20:29:46



Hi All,

I'm a new Unix Admin at our site, and I've had got the following
problem:

We have a script that sets up a number of aliases to create
environments (EVs and aliases).  This script is 'source'd by .cshrc
for our developers.  It sets the alias to 'source' a script, and is
currently set to read extra parameters.

When we execute this on our site we use everyone's default shell,
which is tcsh - this allows parameters to be passed using source.
However, when csh (which is the standard shell at other sites) is
used, passing other parameters breaks down, because the csh version of
'source' doesn't allow parameters to be passed.

I need to confirm a couple of things, I guess.  I assume that source
simply executes the commands in another script without forking - is
that correct?  Is there any way of achieving this and passing through
extra parameters to the called script?

What are the best books to read to understand all types of shell
scripting?  We have a heterogeneous HP/Sun/Linux environment...

Thanks - Adam...

 
 
 

Newbie: tcsh/csh 'source' command question

Post by stephan bea » Sat, 17 May 2003 20:58:05



> I need to confirm a couple of things, I guess.  I assume that source
> simply executes the commands in another script without forking - is
> that correct?  

That's one way of puting it, yes. It runs the contents of the script in the
current shell/environment, as opposed to in a subshell. This means that a
sources script can change your environment and exit your shell, whereas a
subshelled script cannot.

Quote:> Is there any way of achieving this and passing through
> extra parameters to the called script?

In theory you could do this:

in the to-be-sourced script:

function foo
{
 ....

Quote:}

Now source the script and:
foo one two three four

That's assuming, of course, that csh/tcsh have functions (i only use bash).

Quote:> What are the best books to read to understand all types of shell
> scripting?  We have a heterogeneous HP/Sun/Linux environment...

"Learning the Bash Shell", O'Reilly & Assoc.
(that is the only shell worth using, after all ;)

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

 
 
 

Newbie: tcsh/csh 'source' command question

Post by Barry Margoli » Sat, 17 May 2003 23:41:03




>I need to confirm a couple of things, I guess.  I assume that source
>simply executes the commands in another script without forking - is
>that correct?  Is there any way of achieving this and passing through

As the other poster said, that's correct.

Quote:>extra parameters to the called script?

You can't.  The best you can do is set variables before sourcing, and then
have the sourced script use those variables:

set param1 = xxx
set param2 = yyy
source scriptname

and then in the script, refer to $xxx and $yyy.

--

Genuity Managed Services, a Level(3) Company, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

1. How 'source' a Bourne script from a tcsh command line ?

After a couple hours in DejaNews and manuals  . . .

I'm a relative newbie to UNIX & shell scripts.
I'm using Solaris 2.5.1 with 'tcsh' shell for editing conveinience.

I need a script that sets enviroment variables, and I'd like it in
Bourne style.
Script ('setEV'):

     #!/bin/sh

     EV=ev.txt
     export EV

Problem, how can I execute this from a 'tcsh' command line ?
_Can_ it be executed from a tcsh command line ?

     chmod +x setEV
     setEV

no err messages, but then trying

     echo $EV
     undefined variable

Ok, I think, I need to 'source' it, to set the calling process, instead
of the child process enviroment variables.

     source setEV
     EV=ev.txt
     unknown command
     export EV
     unknown command

Obviously 'source' tries to execute it as a csh / tcsh script, and it
doesn't work.
(despite "#!/bin/sh" ?)

_Can_ 'export' be used to set enviroment vars. for a 'csh' 'tcsh' shell
?
Or will it only work for a Bourne / Korn shell ?

I know using 'setenv' in a 'csh' script works, but all I read indicates
that
Bourne shell is a better shell for doing scripts.
(and Perl is better still, and I'll get to Perl eventually)

25 line sig here, not
soren

2. 26 second delay

3. csh/tcsh: calling `source' from within a script

4. IMAP

5. csh: how to 'pre-source' something?'

6. A useful command:

7. Q:sh:Setting env by reading other files - like csh 'source'

8. PortSentry Logging?

9. why does 'which' source my csh?

10. Piping results of a 'find' command to a 'mv' command...

11. ksh 'let' command in csh

12. Csh and ``exit'' command

13. tcsh: How to make 'complete' accept multiple command-patterns?