Jump to content

Csh/tcsh Sanity Check


Guest xionmark

Recommended Posts

Guest xionmark

Hi gang,

Need a reality check here ...

I'm boppin' around working a number of csh/tcsh, bash and Perl scripts and ran into something *really* weird for my burned out central nervous system ... <_<

I'm on a RH7.3 machine, 2.4 kernel (though the sys admin say it's RH EN3 ...) and can't seem to get some env vars to be seen outside of the script they get set in ... almost as if a sh/bash script hadn't used the EXPORT command.

At first I thought it was a matter of a login shell vs. an interactive session, it doesn't seem to be the issue, thus my need for a sanity check.

Here's a "test" script:

#!/bin/csh

setenv MY_VAR foo_bar

But then ...

[mstory@GOTHMOG17 ~/dev]$ echo $MY_VAR
MY_VAR: Undefined variable.

Huh? I admit I'm very tired so I must be missing something increadibly obvious ... there's custom site specific env var's that gets set correctly in my .cshrc but that's a login shell. And when I do:

[mstory@GOTHMOG17 ~/dev]$ source  foo.csh
foo_bar
[mstory@GOTHMOG17 ~/dev]$ echo $MY_VAR
foo_bar
[

What am I'm missing?

(Damn, I need sleep!)

Thanks in advance!

--Mark

Link to comment
Share on other sites

Hi Mark,

Get some sleep! :P

Well... I'm not sure I see the problem, since this sequence

[mstory@GOTHMOG17 ~/dev]$ source  foo.csh
foo_bar
[mstory@GOTHMOG17 ~/dev]$ echo $MY_VAR
foo_bar

seems to suggest that the script foo.csh is setting the envar MY_VAR to the value "foo_bar" as you expect, no?

In any case, I remember that when we were using RH we sometimes had to log out and back in sometimes because the system seemed to hang on to old environment settings and wouldn't update them no matter what. So maybe try that.

Link to comment
Share on other sites

Guest xionmark
Get some sleep! :P

Sleeeeep ... so rare these last few years .... ;)

<SNIP>

seems to suggest that the script foo.csh is setting the envar MY_VAR to the value "foo_bar" as you expect, no?

Yes, that I would expect that from sourcing the script, but I need to execute the script from the command line, with the script living in a sys wide dir, i.e. /usr/local/bin, etc.

And I don't *seem* to be able to remember that I had to source the script ... I should be able to just "run it" ...

In any case, I remember that when we were using RH we sometimes had to log out and back in sometimes because the system seemed to hang on to old environment settings and wouldn't update them no matter what. So maybe try that.

19869[/snapback]

I tried that just now, no difference.

Let me ask the question this way:

When the script is executed in this manner (having it in the $path var path), is it acting like as if the commands were being executed in a "sub shell" and thus exits with the env in the same state as when it started?

Damn, I can't believe I'm all foo-barred on this one ... Should stick C/C++ for everything! :blink:

On another scripting related note, wouldn't it be nice if csh/tcsh supported functions like bash? That's one thing I really like bash for but writing system scripts in bash doesn't seem to be a good idea yet, there's so much csh out there ... hmmm, I seem to remember ksh as a nice shell too, but it's been some 5 years since I've used that.

--Mark

Link to comment
Share on other sites

Guest xionmark

I can write a wrapper that sources the "real" script and passes the args to it but that's a hack, I gotta figure this one out.

:huh:

--Mark

Link to comment
Share on other sites

Guest xionmark
I can write a wrapper that sources the "real" script and passes the args to it but that's a hack, I gotta figure this one out.

19871[/snapback]

Well, that kind of works, it sets the env vars but I can't cd to the desired directory (in the script), I end up back where the script was executed from.

My brain hurts ...

--Mark

Link to comment
Share on other sites

Guest xionmark

Now if I follow everything (setenv's, cd's, etc.) with a new csh, the new shell inherits all the env vars, and I'm in the right directory.

But I don't remember having to do that before. I guess this will work for the moment, but am a little puzzeled ... still working on it. I've got some scripts on my ole trusy IRIX box I'll check.

Thanks Mario!

--Mark

Link to comment
Share on other sites

Hi Mark!

Like you have written in one of your last posts, executing a shell script will do this in a "sandbox". That means, that your current shell will be cloned and the script commands will be executed in the clone-process. So after execution of the script, your shell won't be affected by the script. Writing a wrapper script, that will then do a source, won't do it either.

If it is simply to save typing, you could set an alias (don't know, if this is possible in a csh/tcsh, because I'm a bash user :- )

Link to comment
Share on other sites

Guest xionmark

Hi Frank,

Yes, that jogged the synaptic net! I created an alias that sourced the script, works like a charm. I'm still puzzled about this a bit though, gotta dig thru my ref books, check other code, etc., but at least now I have a good work around (if not the correct solution).

Thanks!!!

The Houdini community rocks!

--Mark

Link to comment
Share on other sites

Hi Frank,

Yes, that jogged the synaptic net!  I created an alias that sourced the script, works like a charm.  I'm still puzzled about this a bit though, gotta dig thru my ref books, check other code, etc., but at least now I have a good work around (if not the correct solution).

Thanks!!!

The Houdini community rocks!

--Mark

19880[/snapback]

Aliases execute and return to the same shell so everything is flat. The problem is you may pollute your current environment with crap. Oh well.

Running other shell scripts with "source" or other ways causes a sub-shell to be spawned and your script is executed in that sub-shell and all you get is the returning value. Any environment variables that you set in the sub-shell get tossed when that script finishes executing.

Use shell aliases or functions when you want to affect the current shell.

A word of note. It is very important that the first line of any script be #!/bin/tcsh or #!/bin/bash or whatever shell is used to execute your script. this is especially true for bash users in a tcsh environment or visa versa. The sub-shell looks for this first line to tell it what kind of shell it really is.

Just do a google search for subshell. Found this link right away:

http://www.die.net/doc/linux/abs-guide/subshells.html

Link to comment
Share on other sites

Running other shell scripts with "source" or other ways causes a sub-shell to be spawned and your script is executed in that sub-shell and all you get is the returning value.

Sorry, but this is wrong :-( Here is a quote from the bash man page:

. filename [arguments]

source filename [arguments]

Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.

"source" doesn't spawn a new subshell. All is executed in the current shell. If it was in a subshell, sourcing houdini_setup[_bash] would have no effect and the necessary environment variables $HFS, etc. could not be set in the current shell. So "pollution" of the current shell is sometimes wanted ;-)

Link to comment
Share on other sites

Guest xionmark

Yea, sourcing doesn't execute in a sub shell.

All of my scripts start with #!/bin/csh-perl-sh-etc., that's pretty standard.

Creating the alias works fine, thanks for all the suggestions.

--Mark

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...