Not a SHELL SHELL-TCSH SHELL programming (1)

Source: Internet
Author: User
Tags echo command echo info

TCSH shell is different from other shells, because the control structure is more in line with the programming language format. for example, the control structure of the test condition of TCSH is an expression rather than a linux Command. The obtained value is a logical value of true or false. The expression of TCSH is basically the same as that of the C language.
A tcsh shell variable, script,

You can define variables in shell, assign values to variables, and reference script parameters. TCSH uses set, @, and setenv to define a variable. You can also use the same method to define numeric variables and arrays. You can use the @ command to define numeric variables for arithmetic operations, you can use parentheses () and brackets [] to define and reference arrays. the script can also be operated in the same way, but there is an exception. Although the echo command can be used to output the prompt, there is no read command to process the input. On the contrary, it must be redirected to a variable.
2. Script Input and Output

$ <You can define and use variables in the script scope. in the following example, use a text editor to place linux commands such as the value assignment operation and echo in a file. then, you can generate an executable file and execute it in the command line like other commands. Remember to add the executable permission, you must use the chmod command with the u + x parameter or the chmod command with the absolute parameter 700. in the script, you can use the echo command to output data. However, you must use the standard redirect input to read the input into the variable. there is no comparative version of the linux read command in TCSH. remember that the first character in the first line of all TCSH script files must be the "#" character. for example:
#
# Display "hello"
Set string = "hello"
Echo The value of string is $ string
The set command and the redirection symbol are combined with $ <to read any data entered by the user into the standard input. In the following example, the user input is read into the string variable.
% Set string =$ <
Abc
% Echo $ string
Abc
The prompt can be placed in the same line as the echo input. TCSH using a special option-n will remove the carriage return in the output string. The cursor will be kept at the end of the output string.
% Echo-n please enter a string
% Cat hello
#
Echo-n "please enter a string :"
Set string =$ <
Echo "the value of string is $ string"
% Chmod u + x hello
% Hello
Please enter a string: hello
The value of string is hello
%
Operator TCSH

It has a series of standard assignment, arithmetic and relational operations, and function assignment operators such as redirection and background operations.

Function Description
= Value assignment operation
+ = Add first and then assign value
-= Subtract first and then assign a value
* = Multiplication first and then assignment
/= Value after Division
% = Obtain the remainder and then assign a value
++ Auto increment 1
-- Auto-reduction 1
Arithmetic Operators
-Negative number
+ Addition
-Subtraction
* Multiplication
/Division
% Remainder
Relational operators
> Greater
<Less
> = Greater than or equal
<= Less than or equal
! = Not equal
= Equal

The redirection and MPs queue operator TCSH supports redirection and MPs queue operations for standard input and output. If noclobber features are set, rewrite the current file with the redirection operation. The symbol must be used>! Replace>
Four control structures

Like other shells, TCSH also has a series of control structures to control the execution of script commands. the while and if control structures are the most common control structures. switch and foreach are more dedicated control structures. switch is a qualified form of the if condition. Check whether the value is equal to a number in a series of possible values. foreach is a restricted form of the loop structure. browse the Value List and assign a new value to the variable. list of different TCSH control structures:
Conditional Control Structure; Function
If (expression) then if expression is true, execute commands
Endif
If (expression) then if expression is true, execute command1; otherwise, execute command1 command2.
Else
Command2
Endif
Switch (string) can be selected from several replacement commands. The string mode is case pattern:
Commands
Breadsw
Default:
Commands
Endsw
Loop Control Structure: The while (expression) function repeats commands as long as expression is true. commands jumps out of the loop end until expression is false.
The foreach variable (argument-list) iteration loop obtains commands as many parameters as in argument-list (variable is set as the next parameter of the list each cycle; end operation is the same as BSH)
The control structure in TCSH is different from other shells because it is closer to programming language (C ). the value of the TCSH conditional expression is true/false. the main difference between BASH and TCSH is that the structure of TCSH cannot be redirected or output in pipelines.

5. Test expression

() The if and while control structures use expressions for testing. if the expression test result is non-zero (1), it indicates true, while zero (0) indicates false (opposite to BASH ). the test expression can be compared by arithmetic/string, but the strings can only be compared equal and not equal. and the expression must be in brackets. for example:
If (expression) then
Command
Endif
TCSH has a series of operators to test and compare strings respectively. Regular Expressions can contain strings of commands of shell scripts. For example:
If ($ var = ~ [Hh] *) then # string if the variable $ var starts with an uppercase/lowercase Hh
Echo information # Run the command
Endif # End
There are many test file operations that are identical to BASH. For example:
If (-r myfile) then # test whether myfile is readable
Echo info
Endif
Basic operations on the test expression:
String comparison: Function
= Equal or not, returns true if equal
! = Whether it is not equal. If it is not equal, true is returned.
= ~ String and mode to test whether it is equal (the mode is any regular expression)
!~ Whether the string and mode test are unequal (the mode is any regular expression) file test: Function
-E: whether the test file exists
-R test file readable
-W test file writable
-X test file executable
-D: test whether the file name is a directory.
-F: whether the test file is a common file
-O whether the test file is owned by the user
-Z: whether the test file is empty
Logical operators: Functions
& And operation. Two conditions are in the same barbarian family.
| Or operation. One condition is met.
! Invert


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.