In *unix systems, commonly used shells include sh, bash, csh/tcsh, ksh.
sh comes from Unix in systemV, and is a traditional Unix shell. Until now, many system administrators still like to use sh.
bash comes from BSD Unix, and the syntax is very similar to C language, so developers with a C/C++ programming background usually like to use it the most.
ksh is an extension of sh and absorbs some useful functions of csh. However, since the license of ksh was AT&T at the beginning, many open source versions of ksh appeared later, such as mksh, pdksh, etc.
bash is the default shell in many Linux distributions nowadays, and it combines many advantages of other shells.
The following describes some basic syntax of csh (execute csh in the sh environment and switch to the csh environment):
1) Variable
Set to define the local variable x, and use the value of variable x through $x or ${x}. $%x represents the length of the value of the variable. $?x is used to determine whether the variable x is set. If set, it is 1, otherwise it is 0.
set x = 5
echo $x
echo ${x}kg
echo $%x
Definition of global variable setenv v value This variable will be inherited by all child shells derived from this shell.
$$ represents the PID of the current process, $status or $? represents the exit status.
2) Array
Define the array myarr, access the value in the array through $myarr[index], note that index starts from 1. Access all elements of the array through $myarr or $myarr[*]. Check the number of elements through $#myarr.
set myarr = (str1, str2,str3)
echo $myarr[2]
echo $myarr
echo $myarr[*]
3) Command replacement
The command is executed by setting x = `cmd` and the result is assigned to the variable.
set d = `date`
echo $d
echo $d[6]-$d[2]-$d[3]
4) Command line parameters
Access command line parameters through $argv[1], $argv[2] or $1,$2. The number of command line parameters is $#argv.
5) Metachars for file name extension
Only use?,*,[abc],[a-c].
6) IO redirection and pipeline
Redirect the output of the command to the file as >.
Redirect and append the output of the command to the file as >>.
Redirects the command input to the file as <.
Redirect the command error message to a file (cmd>/dev/tty)>&errors.
Redirect the command output and error output separately (cmd > goodstuff) >& badstuff.
Redirect the command output and error message to a file cmd>&file.
Send the output of the command through the pipeline to another command cmd|cmd.
Send the command output and error information to another command cmd|&cmd through the pipeline.
The conditional statement is cmd && cmd or cmd || cmd.
command<<WORD means redirecting the input of command to content from the first WORD to the next WORD (i.e. the document here).
7) Read from the keyboard and save to variables
set var = $<
8) Arithmetic
@ var = 5 + 5
echo $var
@ v2 = $var + 3
echo $v2
9) Generation symbol extension
~username represents the username's home directory.
10)Alias
alias m more Create an alias m for more.
alias List all alias.
unalias m Used to delete the alias definition of more.
11) Initialize the file
.login file executed at login.
.cshrc file that executes every time the shell is called.
12) label and goto
There is no concept of functions in CSH, and it uses label and goto similar to Windows batch processing.
goto label
......
label:
....
13) if/else/switch/case
if(expression)then
commands
endif
if {(command)} then
commands
endif
if(expression) then
commands
else if(expression) then
commands
else
commands
endif
switch("$value")
case pattern1:
commands
breaksw
case pattern2:
commands
breaksw
default:
commands
breaksw
endsw
14 while/foreach
while(expression)
commands
continue
break
end
foreach var (wordlist)
commands
end
15、repeat
repeat means repeat execution of the following command.
repeat 3 "echo helloworld"
16. Methods for setting environment variable PATH in csh
Use path instead of PATH in csh, setting similar to the use of arrays.
set path = ($path /home)
echo $path
echo $PATH
17. Source is equivalent to other shells.
source causes the program to be executed in the current shell, rather than derive child processes to execute.
18. Escape characters and odd and double quotes
Quotes must appear in pairs and must be paired on the same line. You can use a backslash to escape the newline character so that you can pair it on the next line.
Single quotes can be used to protect double quotes, and double quotes can also be used to protect single quotes.
Single quotes protect all metacharacters except historical characters (!) from being interpreted.
Double quotes protect all metacharacters except historical characters (!), variable replacement characters ($), and backquotes (for command replacements), so that they are not interpreted.
19. History command
history is used to view the history of command execution.
!! Used to execute the previous command.
20. Pushd and popd are used to maintain the directory stack
21. csh -vx is used to display the input as-is and variable replacement scripts to help debug.
22. Handle interrupts in scripts
onintr finish
<script continues here>
finish:
onintr - # Disable further interrupts
echo Cleaning temp files
exit 1
The onintr command is followed by a label name, and finish is a user-defined label. If an interrupt occurs, control will be transferred to the finish label. Usually this line is at the beginning of the script. Unless ctrl+C (interrupt key) is pressed while the program is executing, the control will be transferred to this label. onintr - means blocking all interrupts. Pressing ctrl+C at this time will be ignored.
23. noclobber prohibits overwriting variables, set the $noclobber preset variable to change the output redirection characteristics.
variable setting syntax set noclobber
Unset noclobber
The function of this noclobber variable is to stop overwiring the existing file by the redirecting symbol ">" and the character ">>" will be written to a non-existent file, which will automatically generate the characteristics of the file.
Only two examples are used to let readers understand the actual usage status after setting.
Example 1:
% ps axu > testfile
% set noclobber
% echo "test set noclobber" > testfile
testfile: File exists.
% echo "test set noclobber" >! testfile
%
Example 2:
% set noclobber
% cat /etc/passwd >> nopass
nopass: No such file or directory
% cat /etc/passwd >>! nopass
%