Hamilton LaboratoriesHamilton C shell 2012User guideTutorials

Scripts

Oregon Coast

Scripts
Previous | Next

Topics

Scripts
Arguments to a script
ignorestatus
source statement
See also

Scripts

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:

512 D% cat >trythis.csh echo hello from trythis ^Z 513 D% trythis hello from trythis

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 script

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:

514 D% cat >tryargv.csh echo $#argv $argv ^Z 515 D% tryargv hello how are you 4 hello how are you

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.)

ignorestatus

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:

516 D% set ignorestatus = 0 517 D% rcode 10 10 csh: The child process running 'rcode' exited with a non-zero status = 10.

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:

518 D% cat >trythis.csh calc ignoreerrors set ignorestatus = 0 rcode 10 echo doesn^'t print ^Z 519 D% trythis 0 10 csh(d:\Nicki\trythis.csh:line 3): The child process running 'rcode' exited with a non-zero status = 10. > in d:\Nicki\trythis.csh < called from line 519 csh: The csh script file 'd:\Nicki\samples\trythis.csh' exited with a non-zero status = 10.

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:

520 D% set ignorestatus = 1

source statement

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:

521 D% cat >trythis.csh echo argv = $argv, threadid = $threadid ^Z 522 D% echo $threadid 6 523 D% trythis hello world argv = hello world, threadid = 7 524 D% source trythis hello world argv = hello world, threadid = 6 526 D% _

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:

526 D% h 5 522 echo $threadid 523 trythis hello world 524 source trythis hello world 525 echo argv = $argv, threadid = $threadid 526 h 5

See also

Sample scripts
Procedures
Aliases
Order of evaluation
Compatibility
Tutorial: Interrupts
Tutorial: Procedures
Tutorial: Aliases

Previous | Next

Getting started with Hamilton C shell

Hamilton C shell, as it first wakes up.

Getting started with Hamilton C shell

A first few commands.

You can set the screen colors to your taste.

You can set the screen colors to your taste.