Introduction to TCSHshell programming

Source: Internet
Author: User
Tags echo command echo info
Article Title: Getting started with TCSHshell programming. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Introduction
TCSH shell is different from other shells, because the control structure is more in line with the format of the programming language. for example
The control structure of the test condition of TCSH is an expression instead of a linux command. the logical value is true.
Or false. the TCSH expression is basically the same as the expression in C language.
  
I. TCSH shell variables, scripts, parameters
You can define variables in shell, assign values to variables, and reference script parameters. use set, @, and setenv for TCSH.
You can also use the same method to define numeric variables and arrays.
For arithmetic operations, you can use parentheses () and square brackets [] to define and reference arrays. the script can also use the same square
But there is an exception. although The echo command can be used to output the prompt, but there is no read command to process the input
Reverse, must be redirected to a variable.
  
2. script input and script output: $ <
You can define and use variables in the script scope. in the following example, use the text editor to assign values and echo
Such linux commands are placed in a file. then, executable files can be generated and stored like other commands.
To execute the command in the line, 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.
Number of chmod commands. in the script, you can use the echo command to output data.
Quasi-input reads the input into the variable. TCSH does not have a comparison version of the linux read command. remember all TCSH scripts.
The first character of the first line of this file must be the "#" character.
For example:
#
# Display "hello"
Set string = "hello"
Echo The value of string is $ string
  
Combine the set command with the redirection symbol $ <将用户输入的任何数据读入标准的输入中.下例中,把用户
The input is read into the string variable.
% Set string =$ <
Abc
% Echo $ string
Abc
  
The prompt can be placed in the same row as the echo input. TCSH uses a special option-n, which will eliminate the input
The carriage return character in the output string. the cursor is retained 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
%
  
3. Operators
TCSH has a series of standard values, arithmetic and relational operations, as well as functions 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
Redirection and pipeline operator
TCSH supports redirection and pipeline operations for standard input and standard output. if noclobber features are set, use
Rewrite the current file with a symbol>! Replace>
  
IV. Control Structure
Like other shells, TCSH also has a series of control structures to control the execution of script commands. while and if control
Structure is the most common control structure. switch and foreach are more dedicated control structures. switch is the if condition
To check whether the value is equal to a number of possible values. foreach is a cyclic structure.
Form of restriction. view 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
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 different.
Case pattern:
Commands
Breadsw
Default:
Commands
Endsw
  
Loop Control Structure: function
While (expression) if expression is true, execute commands again,
Commands jumps out of the loop until expression is false.
End
  
The foreach variable (argument-list) iteration loop obtains the same number of parameters as the argument-list.
Commands (each cycle variable is set as the next parameter of the list;
End operation method is the same as BSH)
  
The control structure in TCSH is different from other shells because it is closer to the conditional expression in programming language (C). TCSH.
The main difference between BASH and TCSH is that the structure of TCSH cannot be redirected or pipelines.
Output.
  
5. Test expression ;()
The if and while control structures use the expression as a test. the test result of the expression is non-zero (1), which indicates true, but zero.
(0) indicates false (opposite to BASH). The Test expression can be compared by arithmetic/string, but the string can only be
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
Command string. 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
  
6. built-in TCSH commands
1. numeric variable :@
In TCSH, replace the "@" command with the "set" command to declare numeric variables, and perform arithmetic, relational, and bitwise operations, numbers, and
String variables are two different objects and need to be managed in different ways. set cannot be used for setting numeric variables.
@ The Command consists of keywords, variable names, value assignment operators, and expressions. for example:
% @ Num = 10 # note space
% @ Sum = 2 * ($ num + 3)
% Echo $ sum
% 26
  
2, alias
Format: alias [name [command]
Command referenced by the keyword alias name
For example:
Alias list ls
Alias list 'ls-L'
  
3, argv
When the script is executed, the words in the command line are analyzed and put into the argv array. argv [0], argv [1]...
Argv [n], where argv [0] saves the command name, argv [1] saves the first parameter of the command
The nth parameter. argv array element can be abbreviated to the element number. Add $. for example: $ argv [1] to the front and write it as $1. $ argv [*].
Enter $ *. # The argv parameter identifier to include the parameter number entered in the command line. check the $ # argv variable. for example:
Arglist
#
Echo "The number of arguments entered is $ # argv"
Echo "The list of arguments is: $ argv [*]"
  
% Tcsh arglist a B c
The number of arguments entered is 3
The list of arguments is: a B c
Argv variable list:
Command line parameter description
$ Argv [0] or $0 command name
$ Argv [n] or $ n the nth command line parameter starting from 1 ($1-$)
$ Argv [*] or $ * all command line parameters starting from 1
$ # Argv or $ # command line parameter count
  
4, bg
Format: bg [% job]
The bg command puts the specified task in the background. if the task has been stopped, continue to execute. if there is no parameter, set
The current task is placed in the background. (the detailed usage is the same as BASH here)
  
5, break
Format: break
The break command is used to exit the newest foreach/while loop. other commands in the same line are executed.
  
6. breaksw
Format: breaksw
It can be interrupted from the switch statement and executed after endsw.
  
7. builtins
List all built-in shell command tables.
  
8, cd
Format: cd [-p] [-l] [-nl-v] [name]
If the directory name is given, this command sets the name to the current directory. if the command does not contain the name, the current directory will automatically
Set it to the user's main directory. "-" for name references the upper-level Directory. if the name parameter does not provide a subdirectory,
Or it is not a full path, or use./or./to reference the current directory and parent directory, then check the shell variable in cdpath.
To find the directory name. if this operation fails, check whether the directory is saved in the shell variable.
Path name.
Use the-p option to display the directory list in linux. use the-l,-n, and-v options to display the directory list exactly the same as those used in the dirs command.
Default option-p.
  
9, continue
This command continues executing the while/foreach statement. The remaining commands in the current line are executed.
  
10, dirs
Format: dirs [-l] [-n |-v]
Dirs-S |-L [filename]
Dir-c
If the parameter is not included, dirs displays the directory list. The list starts with the list and is listed on the working side. The first directory is the current directory.
With The-l option, all subdirectories in the user's home directory are expanded. when the input item reaches the screen edge, the-n option is hidden.
Input items. the-v option displays the input entries for each row. the-c option clears the directory list.
The list is saved as a series of cd and pushed commands in the file. the-L option can be used to set the cd
And pushed command. The file contains the cd and pushed commands stored by the-S option.
Name, which will be assigned to the text in the dirsfile shell variable
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.