Introduction to the basic syntax of CSH _linux Shell

Source: Internet
Author: User
Tags goto

In the *unix system, the commonly used shell has sh,bash,csh/tcsh, ksh.

SH comes from SystemV Unix, is the traditional Unix shell, until now many system administrators still like to use SH.
Bash comes from BSD Unix, the syntax is very similar to C, so developers who typically have a C + + programming background are most likely to use it.
Ksh is an extension of SH and absorbs some useful features of CSH, but since the license of the beginning Ksh is at&t, a lot of ksh open source versions, such as Mksh,pdksh, have appeared later.
Bash is the default shell in many Linux distributions today, combining many of the advantages of other shells.

The following describes some of the basic syntax for CSH (performing CSH switching to the CSH environment in the SH environment):

1) variable

Define the local variable x by set, use the value of the variable x by $x or ${x}, $%x the length of the value of the variable, $?x to determine whether the variable x is set, if the setting is 1, or 0.

Copy Code code as follows:

Set x = 5
Echo $x
Echo ${x}kg
Echo $%x

The definition of a global variable setenv v value the variable will be inherited by all child shells derived from this shell.

$$ represents the PID of the current process, $status or $? Indicates an exit status.

2) array

Defines the array Myarr, $myarr[index] to access the values in the array, noting that index is starting from 1. Access all elements of an array through $myarr or $myarr[*]. Through $ #myarr来查看元素的个数.

Copy Code code as follows:

Set Myarr = (str1, STR2,STR3)
Echo $myarr [2]
Echo $myarr
Echo $myarr [*]

3) Command replacement

Executes the command through set x = ' cmd ', and the result is assigned to the variable.

Copy Code code as follows:

Set d = ' Date '
Echo $d
echo $d [6]-$d [2]-$d [3]

4) Command Line parameters

Through $argv[1], $argv [2] or $1,$2 to access command-line arguments. The number of command line arguments is $ #argv.

5) The meta character of the filename extension

Can only use?, *,[abc],[a-c].

6 IO Redirection and piping

Redirects the output of the command to a file of >.
Redirects the output of the command and appends it to the file as >>.
Redirects the command's input to a file of <.
Redirects the command's error message to a file (Cmd>/dev/tty) >&errors.
Redirects the output and error output of the command separately (cmd > Goodstuff) >& badstuff.

Redirects the output and error information of the command to a file cmd>&file.
The output of the command is piped to another command cmd|cmd.
The output of the command and the error message are piped to another command cmd|&cmd.
Conditional statements are cmd && cmd or cmd | | Cmd.
Command<<word indicates that the command's input is redirected to the content (that is, the here document) that starts at the first word and between the next word.

7 read from the keyboard and save to the variable

Copy Code code as follows:

Set var = $<

8) arithmetic

Copy Code code as follows:

@ var = 5 + 5
Echo $var
@ v2 = $var + 3
Echo $v 2

9) Symbolic extension of the character

~username represents the username home directory.

10) Alias

Alias m more creates aliases for more.
Alias lists all the alias.
Unalias m is used to delete more alias definitions.

11 Initialization of the file

. login files that are executed at logon time.
The file that the. CSHRC executes every time the shell is invoked.

) Label and Goto

There is no concept of functions in CSH, using label and Goto similar to those in Windows batch.

Copy Code code as follows:

Goto Label
......
Label
....

If/else/switch/case)

Copy Code code as follows:

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

While/foreach

Copy Code code as follows:

while (expression)
Commands
Continue

Break

End
foreach var (wordlist)
Commands
End

15, repeat

Repeat indicates that the following command is repeated.

Copy Code code as follows:

Repeat 3 "echo HelloWorld"

16, the method of setting environment variable path in CSH

CSH uses path instead of path to set up an array-like usage.

Copy Code code as follows:

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 as a derived subprocess.

18, escape character and single double quotation mark

The quotation marks must appear in pairs, and must be paired on the same line. You can use backslashes to escape line breaks so that you can pair them 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 quote protection except for historical characters (!) ) are not interpreted except for all meta characters.
Double quote protection except for historical characters (!) ), variable substitution characters ($) and inverted quotes (for command substitution) for all meta characters 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 used to maintain the directory stack

21, CSH-VX used to display the input of the original and variable replacement script, to help Debug.

22, in the script to handle the interrupt

Copy Code code as follows:

ONINTR Finish
<script continues here>

Finish
ONINTR-# Disable further interrupts
Echo Cleaning Temp Files
Exit 1

ONINTR command followed by a label name, finish is the user's custom label. If an interruption occurs, the control is transferred to the finish label. Typically, the line is at the beginning of the script. Unless you press CTRL + C (break key) while the program is executing, the control is transferred to that label. Onintr-Indicates that all interrupts are masked, and pressing CTRL + C will be ignored.

23, Noclobber prohibit the coverage of variables, set $noclobber preset variables to change the output redirection characteristics.

Variable set syntax set Noclobber

Cancel variable setting syntax unset noclobber

This noclobber variable, its function is to stop the redirection symbol ">" Overwrite (overwiting) existing file and symbol ">>" to write characters to a nonexistent file, automatically generate the characteristics of the file.

Use only two examples to let the reader know the actual usage after Setup.

Example one:

Copy Code code as follows:

% PS Axu > testfile
% Set Noclobber
% echo "Test set Noclobber" > Testfile
Testfile:file exists.
% echo "Test set Noclobber" >! Testfile
%

Example two:

Copy Code code as follows:

% Set Noclobber
% cat/etc/passwd >> NoPass
Nopass:no such file or directory
% cat/etc/passwd >>! NoPass
%

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.