Shell operations and simple programming (6)

Source: Internet
Author: User

Linux Shell Programming

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. The shell program uses $ VaR to reference the value of a variable named 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 of the registration directory.

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, 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, if the exit status of a command is 0, the command is correctly executed. If the value is not 0, an error occurs .)

$ 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.