分类:
2005-09-08 15:06:15
The .cshrc file contains commands, variable definitions and aliases used any time the C shell is run. When one logs in, the C shell starts by reading the .cshrc file, and sets up any variables and aliases.
The C shell reads the .login file after it has read the .cshrc file. This file is read once only for login shells. This file should be used to set up terminal settings, for example, backspace, suspend, and interrupt characters.
The .logout file contains commands that are run when the user logs out of the system.
Sample .cshrc file
#!/bin/cshSample .login file
# Sample .cshrc file
setenv EXINIT 'set smd sw=4 wm=2'
set history=50
set savehist=50
set ignoreeof noclobber
if ($?prompt) then
set prompt='[!]% '
alias f finger -R
alias lo logout
endif
#!/bin/cshSample .logout file
# Sample .login file
stty erase ^H intr ^C susp ^Z
echo "Welcome to Wiliki!"
frm -s n
#!/bin/csh
# Sample .logout file
echo -n "Logged out of Wiliki "
date
set var1=a3 #sets var1's value to a3.
set var2=(a b c)
# sets the array variable var2 to a b, and c.
The existence of variables can be checked using the form $?variable. If the variable exists, the expression evaluates to a one (true), otherwise, it evaluates to a zero (false). Simple integer calculations can be performed by C shell, using C language-type operators. To assign a calculated value, the @ command is used as follows:
@ var = $a + $x * $zCertain variables control the behavior of the C shell, and some of these don't require a value. (I.e., can be set simply by using set command by itself without any value.) The unset command can be used to unset any undesirable variables.
Commands from the history can be recalled using the exclamation point. For example, !! repeats the previous command, !25 re-types command number 25 from the history, and !-2 re-types the second line previous to the current line.
Individual words from these command lines can also be retrieved using this history. For example, !25:$ returns the last argument (word) from command 25, !!:* returns all the arguments (all words but the first one) from the last command, and !-2:0 returns the command (the first word) of the second line previous.
A shorthand can be assigned to a command or sequence of commands which are frequently used. By assigning an alias with the alias command, one can essentially create their own commands, and even "overwrite" existing commands. For example:alias cc cc -Aa -D_HPUX_SOURCEThis alias definition will substitute the cc with the ANSI compiler option on an HP System (such as Wiliki) whenever cc is typed. To undefine an alias, the unalias command is used.
If the filenames used behind an alias must come before text being substituted, history substitution can be used, as follows:
alias manl 'man !* | less -p'This form of the command places the arguments placed after the manl alias between the man command and the | (pipe).
date > datefileThe output of the date command is saved into the contents of the file, datefile.
a.out < inputfileThe program, a.out receives its input from the input file, inputfile.
sort gradefile >> datafileThe sort command returns its output and appends it to the file, datafile.
A special form of redirection is used in shell scripts.
calculate << END_OF_FILEIn this form, the input is taken from the current file (usually the shell script file) until the string following the "<<" is found.
...
...
END_OF_FILE
If the special variable, noclobber is set, if any redirection operation will overwrite an existing file, an error message is given and the redirection will fail. In order to force an overwrite of an existing file using redirection, append an exclamation point (!) after the redirection command. For example for the command:
date >! datefileThe file datefile will be overwritten regardless of its existence.
Adding an ampersand (&) to the end of an output redirection command will combine both the standard error and the standard output and place the output into the specified file.
The output of one command can be sent to the input of another command. This is called piping. The commands which are to be piped together are separated by the pipe character. For example:ls -l | sort -k 5nThis command takes the output of the ls -l command and puts the output of it into the sort command.
By appending an ampersand (&) after the pipe character, one can combine the standard error and standard output and send it to the standard input of the program receiving the piped output.
The C shell handles job control, which allows the user to run certain programs in the background, and recall them to the foreground when necessary. In order to place a running process into the background, the suspend character must be set by the stty command shown earlier. Processes may be started in the background by following the command with an ampersand (&).When a job is placed in the background, information for the job is shown similar to the example given below:
[1] 15934This specifies that the process has been placed in the background, and is job 1. In order to recall jobs placed in the background, the fg command is used, while the bg command places a recently stopped process into the background. The jobs command gives a list of all processes under control of the current shell. Also, typing a percent sign (%) with the job number brings that particular job to the foreground. The C shell has control structures similar to the C programming language. These are foreach, if, switch and while. These are usually used in shell scripts.
There are two forms of the if statement. The first one has a simple command after the expression. This simple command cannot be an alias, nor can it use statements that use the backquote (`). The second form of the if command must have the word, then following the expression. Several if statements can be chained together, through the use of the else statement. This statement must have a corresponding endif statement.
if (expression) simple command
if (expression) then
...
else
...
endif
The switch statement can replace several if ... then statements. For the string given in the switch statement's argument, commands following the case statement with the matching pattern are executed until the endsw statement. These patterns may contain ? and * to match groups of characters or specific characters.
switch (string)
case pattern1:
commands...
breaksw
case pattern2:
commands...
breaksw
default:
commands...
breaksw
endsw
The while statement will enter the loop only if the expression evaluates to true (or non-zero). Once within the loop, the commands within it will continue to execute until the expression evaluates to false (zero).
while (expression)
commands...
end
The foreach statement takes an array variable and places the contents of each array element into the loop variable for each iteration.
foreach variable (array variable or list)
...
end
The break statement breaks out of the current loop.
break
The continue command returns to the top of the current loop after testing the condition for the loop.
continue
The shift command without arguments will shift the variable, argv down by one element. In other words, argv[2] becomes argv[1] and so forth, with argv[1] being discarded. With an array variable argument, the shift command performs the same operation on the variable specified.
shiftThe expressions used in the while and if commands are similar to C language expressions, with these exceptions:
shift variable