How to make output behave when "sourced" in csh

csh is an absolutely terrible shell. (Forcing this expression to avoid the inevitable “don't use csh” comments in advance, or maybe I should just say for the sake of any readers: please, if you value your sanity, don't use csh .) I have a pretty large collection of csh scripts, which I am trying to make useful, and I just found that exit not called from a script when called from the source file. In other words:

 #!/bin/csh source [file with content "exit"] echo No reasonable script should print this 

Produces a conclusion.

Is there any way to change this behavior? As far as I can tell, the original authors of the scripts that I modify assumed that the main script is finished, and that, of course, behavior would be desirable, and just calling the example above via sh produces this result, but what gives? Is there a reasonable purpose for this behavior? (What I'm just writing is the real question: is there a csh option to change the behavior?)

+4
source share
5 answers

To exit the script source and the parent interpreter in csh / tcsh, I use exec /bin/false or exec /bin/true , depending on the success condition that I want to pass to the parent process. The original shell exits with the corresponding return value.

+3
source

I do not think this refers to the original context of the question, but if exit used to close the script input and therefore exit the system, you can simply use logout instead of exit .

+1
source

Regarding the popular sports community, I call "csh bashing":

"csh is an absolutely terrible shell."

Maybe, but tcsh is a very easy to use, easy to use, lightweight and stable shell, available almost everywhere with pretty uniform features. Although, he has some quirks that upset you until you learn how to navigate them. This helps to have a pretty-printer for long scripts.

The function "source file [argv]" is the closest to the routine in tcsh, and since it shares the same namespace with the caller, it is reasonable that the exit command works more like the "return" statement in this context. Note that the exit statement pauses processing in the source file and returns a value through $ status.

Sourced tcsh files also allow the caller to place a separate $ argv without losing the original or not, in which case the caller $ argv is displayed in the source file. If the source file is written to $ argv, it is also changed for the caller.

If you define an alias that creates the file, you can compile several fairly universal multifunctional scripts:

alias func 'set srcf = "test.tcsf"; set args = (! *); source $ srcf $ args'

However, if the game has several source files, you need to accept some namespace management conventions to ensure that they do not attack each other. On the bottom line, there are reasons why tcsh is still in use after all these years, especially because it is fast and easy to use as soon as you understand the quirks.

+1
source

I don’t claim to know much about csh, I mainly use “bash” and I think that a long time ago in a faraway country I might have used “ksh: on Sun" pizza "boxen. A little noise, I noticed a few csh behvior change switches, but what if you tried something like this:

 exit [expr] The shell exits either with the value of the specified expr (an expression, as described under Expressions) or, without expr, with the value of the status variable. 

I got it from here

0
source

You can output an alias to set some value, and then check it:

 $ cat a.csh #!/bin/csh alias exit 'set abort=1; ""exit' ... exit 

And when searching for a file:

 $ cat b.csh #!/bin/csh source a.csh if ( $?abort ) exit 

Application:

I forgot to say that you can change the source code on the fly:

 $ cat b.csh #!/bin/csh source `sed '2 i alias exit '"'set abort=1; "'""exit'"'" a.csh` if ( $?abort ) exit 

This will have some penalty when comparing to changing the script directly, but you no longer need to worry about the original script.

0
source

All Articles