Shell programming syntax (1) ZT

Source: Internet
Author: User
In fact, the interactive interpretation and execution of user input commands as a command language is only one aspect of the Shell function, Shell can also be used for program design, it provides a variety of methods to define variables and parameters as well as program control structures. Using Shell programming is similar to a batch processing file in DOS, called a Shell script, or a Shell program or Shell command file.

  Shell basic syntax

Like an advanced programming language, Shell also provides instructions and the ability to use variables. For Shell, the values of all variables are a string, Shell Program Reference the value of a variable named var in the form of $ var.

Shell has the following basic types of variables.

(1) environment variables defined by Shell:

At the beginning of execution, Shell has defined some variables related to the system's working environment. You can also redefine these variables. Common Shell environment variables include:

HOME is used to save the full Path Name .

PATH is used to save the directory PATH names separated by colons. Shell searches these directories in the order given in the PATH variable, and the first executable file with the same name as the command will be executed.

The type of the TERM terminal.

The identifier of the current user. The value is a string consisting of digits.

The absolute path name of the current working directory of PWD. The value of this variable varies with the use of the cd command.

PS1 primary prompt. for privileged users, the default primary prompt is #. For common users, the default primary prompt is $.

When the PS2 Shell receives the user's input command, if the user enters "\" at the end of the input line, and then press enter, or when the Shell determines that the command entered by the user is not over when the user presses the Enter key, the auxiliary prompt is displayed, prompting the user to continue entering the rest of the command. The default auxiliary prompt is>.

(2) User-Defined variables:

You can define your own variables according to the following syntax rules:

Variable name = variable value

Note that $ should not be added before the variable name when defining the variable, and $ should be added before the variable name When referencing the variable content; when assigning values to the variable, no space must be left on both sides of the equal sign. If the variable itself contains spaces, the entire string should be enclosed in double quotation marks.

When writing a Shell program, we recommend that you use uppercase letters to indicate all variable names to distinguish them from command names.

Sometimes, when we want to describe a variable and set it to a specific value without changing its value, we can use the following command to ensure the read-only of a variable:

Readonly variable name

At any time, the created variables are only local variables of the Current Shell, so they cannot be used by other commands or Shell programs run by the Shell, the export command can provide a local variable to other commands executed by Shell in the format:

Export variable name

You can also use the export command when assigning values to variables:

Export variable name = variable value

Variables described in export can be accessed in all commands or programs run after Shell.

(3) location parameters:

A location parameter is a variable that is determined by its location in the command line that calls the Shell program. It is a parameter entered after the program name. Location parameters are separated by spaces. Shell replaces $1 with the first location parameter in the program file, and the second with $2, and so on. $0 is a special variable whose content is the file name of the current Shell program. Therefore, $0 is not a location parameter, $0 is not included when all the current location parameters are displayed.

(4) predefined variables:

Predefine variables are similar to environment variables and are also defined at the beginning of Shell. The difference is that you can only use these variables according to Shell definitions, rather than redefinition. All predefined variables are composed of the $ operator and another symbol. Common Shell predefined variables include:

$ # Number of location parameters.

$ * Content of all location parameters.

$? Status returned after the command is executed.

$ Process ID of the current process.

$! The last process number that runs in the background.

$0 indicates the name of the currently executed process.

Where, $? Used to check whether the previous command is correctly executed. (In Linux The exit status of the command is 0, indicating that the command is correctly executed, and any non-0 value indicates that the command has an error .)

$ The most common purpose of a variable is to use the name of the temporary file to ensure that the temporary file will not be repeated.

(5) variable for parameter replacement:

Shell provides the parameter replacement function so that you can assign different values to variables based on different conditions. There are four types of parameters for parameter substitution. These variables are usually associated with a location parameter and determine the value of the variable based on whether the specified location parameter has been set to a class, their syntax and functions are as follows.

A. Variable =$ {parameter-word}: If a parameter is set, replace the value of the parameter with the value of the parameter. Otherwise, replace the value with word. That is, the value of this variable is equal to the value of a parameter. If this parameter is not set, the variable is equal to the value of word.

B. variable =$ {parameter = word}: If a parameter is set, replace the value with the value of the parameter. Otherwise, set the variable to word and replace the value with word. Note that location parameters cannot be used in this way, because location parameters cannot be assigned values in Shell programs.

C. Variable =$ {parameter? Word}: If a parameter is set, replace the value of the variable with the value of the parameter. Otherwise, word is displayed and exited from Shell. If word is omitted, standard information is displayed. This variable must be equal to the value of a parameter. If this parameter is not set, a message is displayed and then exited. Therefore, this method is often used to indicate errors.

D. Variable =$ {parameter + word}: If the parameter is set, replace the variable with word. Otherwise, do not replace the variable.

In all the four forms, the "parameter" can be either a location parameter or another variable, but there are many situations where the location parameter is used.

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.