See the UNIX Programmers Manual or via % man csh
% csh filename
will begin a new shell, process the file .cshrc if present, and then
issue the lines present in filename as if they were being typed in.
At end of file, CTRL-D terminates the new shell and returns to
the % level.
% csh -x scriptfile
The verbose and echo options and the related -v and -x command line options
can be used to help trace the actions of the shell. The -n option causes the
shell only to read commands and not to execute them and may sometimes be
of use.
One other thing to note is that in 4.2bsd, csh shell scripts start with
the characters
#! /bin/csh
on the first line of the script.
Similarly, in 4.2bsd, Bourne shell scripts start with the characters
#! /bin/sh
on the first line of the script.
This allows shell scripts for both shells to live in harmony.
% (foo > myfile) >& errfile
is the method for separating stderr messages from the stdout output.
In the example, the standard output of the command named foo will be stored
into the file named 'myfile', but the stderr messages will be stored into
the file named 'errfile'.
CSH COMMAND OPTIONS
If argument 0 to the shell is `-' then this is a login shell.
The flag arguments are interpreted as follows:
-c
Commands are read from the (single) following argument which must be
present. Any remaining arguments are placed in argv.
-e
The shell exits if any invoked command terminates abnormally or yields
a non-zero exit status.
-f
The shell will start faster, because it will neither search for nor
execute commands from the file `&.cshrc' in the invoker's home
directory.
-i
The shell is interactive and prompts for its top-level input, even if
it appears to not be a terminal. Shells are interactive without this
option if their inputs and outputs are terminals.
-n
Commands are parsed, but not executed. This may aid in syntactic
checking of shell scripts.
-s
Command input is taken from the standard input.
-t
A single line of input is read and executed. A `\e' may be used to
escape the newline at the end of this line and continue onto another
line.
-v
Causes the verbose variable to be set, with the effect that command
input is echoed after history substitution.
-x
Causes the echo variable to be set, so that commands are echoed
immediately before execution.
-V
Causes the verbose variable to be set even before `&.cshrc' is
executed.
-X
Is to -x as -V is to -v.
After processing of flag arguments if arguments remain but none of the
-c, -i, -s, or -t options was given the first argument is taken as the name of
a file of commands to be executed. The shell opens this file, and saves its
name for possible resubstitution by `$0'.
Since many systems use either the standard version 6 or version 7 shells
whose shell scripts are not compatible with this shell, the shell will
execute such a `standard' shell if the first character of a script
is not a `#', i.e. if the script does not start with a comment.
Remaining arguments initialize the variable argv.
FILE INQUIRIES:
-r read access
-w write access
-x execute access
-e existence
-o ownership
-z zero size
-f plain file
-d directory
There are four ways to execute a csh script: 1) by aliasing it via
alias, 2) by placing it in the 'PATH' variable for csh to find, 3) by
using its name as an argument to a new csh shell, 4) by using its name
as an argument to the 'source' command.
The alias and PATH methods additionally require the script be
marked as 'executable' with:
% chmod ugo+x zrm
1) To alias the command, place the following line in your .login file:
alias zrm '~/bin/zrm \!*'
where ~/bin is the directory which contains the executable version of
zrm (you can substitute your own name here; ~/bin is a good name
because it is a fairly wide-spread UNIX convention). If you don't
already have a directory named bin, you can make one by:
% mkdir ~/bin
Now, after you place it into that directory, THE NEXT TIME YOU LOGIN
the zrm command will be invokable by name.
2) To use the PATH method, first mark zrm as executable, then place
the script in one of the directories specified in the "set PATH" line
of your .login file. Then, THE NEXT TIME YOU LOG IN, the zrm command
will be executable simply by typing the name zrm at the % prompt.
Normally you will want to execute it without logging off and logging
back on again, and this can be accomplished by typing
% rehash
The rehash command causes csh to regenerate the hash table in which it
finds commands; this is always accomplished at login time, so if any
of the directories are modified with new scripts, then the hash table
will not reflect that fact until the rehash command is executed. The
one exception to this mechanism is that you can usually execute a
script in your current working directory (because the set of
directories included in the PATH variable usually includes '.' as its
first entry.)
3) For the method of calling csh with an argument, use:
% csh zrm
-OR-
% csh -v zrm
where 'zrm' is in the current working directory; if it resides
elsewhere, then use the fully qualified name here instead of zrm.
This method fires up a new csh shell, which reads its input
directly from the script, as if you had typed it in from the key-
board. The second variation shown (the -v flag) will additionally
force the execution to happen in 'verbose' mode. This means that the
command input is echoed (listed) after history substitution. Also,
the -n flag is sometimes useful early in debugging since it will
cause the shell to interpret the commands and echo them, but actual
execution is suppressed; this will help catch gross syntax errors.
4) Finally, the command 'source' can be invoked like this:
% source zrm
where 'zrm' is in the current working directory; if it resides
elsewhere, then use the fully qualified name here instead of zrm. The
source command is NOT USUALLY appropriate though, because it exists
specifically for cases in which a script creates a new shell variable
or modifies the value of an existing variable. This exception is
necessary because spawned shells by default DO NOT change variables in
the login shell. The source command exists solely to accomplish this.
#! /bin/csh # csh argument examples
echo 'All arguments:' $argv[*]
echo 'Number of arguments:' $#argv
echo 'Each argument:'
foreach i ( $argv[1-] )
@ argnum ++ # set and increment csh user variable
echo '$argv[' $argnum '], Argument:' $i
end
# Demonstration of execution:
#
# % x one 2 three four
# All arguments: one 2 three four
# Number of arguments: 4
# Each argument:
# $argv[ 1 ], Argument: one
# $argv[ 2 ], Argument: 2
# $argv[ 3 ], Argument: three
# $argv[ 4 ], Argument: four
# %
#! /bin/csh # executing ex commands from a file
ex - filename << 'EOF'
1,$s/^[ ]*//
w
q
'EOF'
This csh script runs the editor to delete leading blanks from the
lines in the file 'filename'.
The notation << 'EOF' means that the standard input for the ex command
is to come from the text in the shell script file up to the next line
consisting of exactly 'EOF'.
The fact that the `EOF' is enclosed in `' characters, i.e. quoted,
causes the shell to not perform variable substitution on the intervening
lines.
In general, if any part of the word following the `<<' which the shell
uses to terminate the text to be given to the command is quoted then
these substitutions will not be performed.
In this case since we used the form `1,$' in our editor script we
needed to insure that this `$' was not variable substituted. We could
also have insured this by preceding the `$' here with a `\', i.e.:
1,\$s/^[ ]*//
but quoting the `EOF' terminator is a more reliable way of achieving the
same thing.
% echo this shows how to access the last argument of the prior command:
this shows how to access the last argument of the prior command:
% echo !$
echo command:
command
%
WARNING:
The if statement REQUIRES a space character following it.
The following IS WRONG:
if($#argv == 2) then ...
and SHOULD BE:
if ($#argv == 2) then ...
If your endifs are not working correctly, this could be your problem!
> From: stan@teltone.UUCP
#! /bin/csh # indexing through a file with csh
# Here's 3 ways of indexing through a file's contents within a
# C shell script. Assume the file name is "file".
# (you can even write all the below to some file and run csh on it)
echo "Method 1"
set f = ("`cat file`") # makes f a list of elements, each being a
# line of file
# foreach line ($f) # note that you can't do it with this foreach loop.
# echo "$line"
# end
@ linenum = 1
while ($linenum <= $#f)
echo "$f[$linenum]" # gets line, including embedded white space
# echo $f[$linenum] # gets line, embedded white space collapses
# to single spaces.
@ linenum++
end
# echo "$f[1] # gets the first line of the file
# echo "$f[$#f]" # gets the last line of the file
#
# limitation on above is the length of the file. (approx. 10,290 bytes)
#
echo "Method 2"
set linecount = `wc -l < file` # have to use '<' here.
@ linenum = 1
while ($linenum <= $linecount)
set line = "`awk 'NR == $linenum {print;exit}' file`"
# above makes $line have a single string value.
# note: $linenum will be expanded properly
echo "$line"
@ linenum++
end
# similar to above, but use 'sed', which might be (and probably is)
# faster than awk.
echo "Method 3"
set linecount = `wc -l < file`
@ linenum = 1
while ($linenum <= $linecount)
# 2 backslashes at end of each of next 2 lines
set line = "`sed -n '$linenum{p\\
q\\
}' file`"
# can't put comments after 2 lines with backslashes above
echo "$line"
@ linenum++
end
> From Ken Turkowski ARPA: turtlevax!ken@DECWRL.ARPA
Subject: Re: How to get the C-shell to recognize -? as a command flag?
PROBLEM: In trying to make my shell scripts understand a standard help
inquiry, I would like to have the csh recognize the flag "-?" , so that
one might say:
% gobble -\?
and have it echo:
gobble takes the flags:
-v Verbose
-o Output placed in rather than default
-? Prints this help message
SOLUTION:
#! /bin/csh # recognize flag -? from a csh script
switch ("$argv[1]")
case '-[?]':
breaksw
case '-a':
endsw
THE CSH COMMANDS:
@ (1) - csh: mechanism to manipulate numeric variables
alias (1) - csh: mechanism to create or list command-aliases
alloc (1) - csh: debugging command to show amount of dynamic core in use
bg (1) - csh: force a job into background
breaksw (1) - csh: part of the case construct in csh scripts
cd (1) - csh, sh: change working directory
chdir (1) - csh: same as cd
continue (1) - csh: continue executing the nearest enclosing while/foreach
csh (1) - csh: a shell (command interpreter) with C-like syntax
dirs (1) - csh: lists the directory stack of pushd & popd commands
echo (1) - csh: echo arguments
end (1) - csh: ends a foreach statement in csh scripts
endif (1) - csh: ends an if statement in csh scripts
endsw (1) - csh: ends an switch statement in csh scripts
eval (1) - csh: method to EXPORT environment variables to csh shells
exec (1) - csh: execute specified command in place of the current shell
fg (1) - csh: cause a background job to return to foreground
foreach (1) - csh: method of looping in csh scripts
glob (1) - csh: command like echo for making filename expansions
hashstat (1) - csh: determine effectiveness of internal hash table
history (1) - csh: invoke or manipulate the csh history 'event list'
if (1) - csh: method to branch in a csh script
jobs (1) - csh: list jobs running in background
limit (1) - csh: limit system resources
login (1) - csh: sign on the system
logout (1) - csh: sign off the system
nice (1) - csh: run a low-priority process
nohup (1) - csh: execute command immune to hangup, terminate signals
from the controlling terminal
notify (1) - csh: asynchronously notify you of change of job status
popd (1) - csh: manipulate the csh directory stack
pushd (1) - csh: manipulate the csh directory stack
rehash (1) - csh: cause the executable directory-search to be updated
repeat (1) - csh: repetitive invocation of command(s)
set (1) - csh: list shell variables or set a shell variable to a value
setenv (1) - csh: list environment variables or set environment variable
shift (1) - csh: shift members of argv to the left, discarding argv[1]
source (1) - csh: execute a shell script so that environment changes
become effective in the current shell
stop (1) - csh: stop execution of a background job
suspend (1) - csh: suspend a shell; used to stop shells started by su
switch (1) - csh: part of the case construct in csh scripts
time (1) - csh: time a command
umask (1) - csh: display or set the file creation mask for access modes
unalias (1) - csh: reverse the effects of alias for a specified alias
unhash (1) - csh: disable using internal hash table
which (1) - csh: locate a program file including aliases and paths
CSH PRE-DEFINED AND ENVIRONMENT VARIABLES:
argv set to the arguments to the shell
cdpath lists alternate directories to search for in cd command
cwd the full pathname of the current directory
echo causes commands and arguments to be echoed before executed
histchars to change the history substitution characters '!' and '^'
history give a numeric value to control the size of the history list
home home directory of the invoker, initialized from the environment
the environment variable HOME
ignoreeof set to make shell ignore end-of-file from terminals
mail the files where the shell checks for mail
noclobber restrict redirection so files aren't accidentally destroyed
noglob if set, filename expansion is inhibited
nonomatch not an error for filename expansion to not match existing files
notify if set, the shell notifies asynchronously of job completions
path specifies a directory in which commands are sought for execution
prompt prompt string which is printed before each command ('%')
savehist a numeric value controls number of entries of the history list
shell the file in which the shell resides
status the status returned by the last command
term the environment variable TERM
time controls automatic timing of commands
user the environment variable USER
verbose print words of each command after history substitution