Scripts
Arguments to a script
ignorestatus
source statement
See also
Scripts are a final way of bundling up a series of statements to be called up and executed as a single command. To create a script, create a file with a .csh extension:
When you tell the shell to run a script, it first creates a new thread to run it. This is partly a holdover from original UNIX language definition, partly a response to a provision in Windows for threads, but not a fork mechanism and partly due to a genuine need to inexpensively separate some of the script’s environment from that of its caller. (The next chapter has a longer discussion of threads.)
Arguments to a shell script are passed to it as the argv variable. argv will be a list of any words that appeared on the command line following the name of the shell script. (You can access the name of the script as the scriptname variable.) You can access argv like any other variable:
There are also some shorthand forms for getting individual words of argv. $0 through $9 is the same as $argv[0] through $argv[9]. (Remember that unless you have nullwords set, subscripting errors will be caught.)
If you write a script with serially connected statements the only thing that would cause the shell to quit before it gets to the end would be an explicit failure: an application name that couldn’t be found, a child process that terminated with a segment fault, or something else of an equally serious nature. Often in a script, that’s not what you want: you’ve written the script with the expectation that everything will work (as you planned) from one step to the next. If something is wrong, you’d like the script to quit as soon as possible, before any damage is done.
The way you do this is by setting ignorestatus = 0, which means you do not want to ignore the status codes coming back to this thread from its children. Here’s an example in the main thread:
In the main thread, the shell will keep on going and prompt for the next command because interactively that’s most sensible. The shell knows to do this because ignoreerrors = 1. But in a script, errors cause the shell to quit:
Notice that in this case we got two messages, one from the threads executing the script and one from the main thread, reporting what the script returned. Let’s return to the normal mode of ignoring status:
The examples so far have shown how a script is normally run somewhat isolated in a separate thread. It is also possible to run a script in your current thread using the source statement. You might want to do this if you wanted to the script to change your current thread’s private variables or its current directories or disk. Here’s an example to showing how a sourced script runs in the same thread:
Notice how the argv argument vector is set up the same in either case. Also, notice that the statement number skipped by one. When you source a script, the effect is precisely as if you typed those lines in directly to the shell. The lines read by source are even entered into the history list:
Sample scripts
Procedures
Aliases
Order of evaluation
Compatibility
Tutorial: Interrupts
Tutorial: Procedures
Tutorial: Aliases