Shell operation and simple programming (6)

xiaoxiao2021-03-06  63

Linux's shell programming

In fact, as a command language interactively interprets and executes commands that the user entered is only one aspect of the shell function, and the shell can also be used for programming, which provides a means of defining variables and parameters and a rich program control structure. Use shell to program a batch file similar to DOS, called Shell Script, also called the shell program or shell command file.

Shell Basic Law

Like advanced programming languages, the shell also provides a function of explaining and using variables. For the shell, all variables are all strings, and the shell program uses $ VAR form to reference the value of variables named VAR.

Shell has several basic types of variables.

(1) SHELL defined environment variable:

SHELL has defined some variables related to the working environment of the system when executing, and users can redefine these variables, and the common shell environment variables are:

HOME is used to save the full path name of the registration directory.

Path is used to save the directory path name separated by a colon, and the shell searches these directories in the order given in the PATH variable, and the first executable identical to the command name will be executed.

Type of TERM terminal.

UID The current user's identification word, the value is a string composed of a digit.

The absolute path name of the PWD current work directory, the value of the variable changes with the use of the CD command.

PS1 master prompt, under the privileged user, the default main prompt is #, under normal users, the default principal prompt is $.

PS2 In the process of receiving the user input command, if the user enters "/" in the end of the input row, then the shell determines that the user entered command is not ended when the user presses the check button, or when the user is pressed, it shows this auxiliary tips. The user prompts the user to continue the rest of the command, the default auxiliary prompt is>.

(2) User-defined variables:

Users can define their own variables as follows:

Variable name = variable value

It should be noted that when the variable is defined, the variable is not added to the symbol. When the content of the reference variable should be added to the variable name; when the variable is assigned, the equal sign must not leave the space, if the variable It contains spaces, and the entire string must be enclosed in double quotes.

When writing a shell program, in order to distinguish the variable name and the command name, it is recommended to use uppercase letters in uppercase letters.

Sometimes we want to indicate a variable and set it to a specific value, you can use the following command to ensure the readiness of a variable:

Readonly variable name

At any time, the created variable is just a local variable of the current shell, so it cannot be utilized by other commands or shell programs running by the shell, and the export command can use a local variable to use other commands executed by the shell, its format:

EXPORT variable name

You can also use the export command while assigning the variable:

EXPORT variable name = variable value

Use the variables illustrated by Export, all commands or programs running after the shell can be accessed.

(3) Location parameters:

The location parameter is a variable determined by the respective position in the command line that call the shell program, is the parameter entered after the program name. The position parameters are separated by spaces, and shell takes the first position parameter replacement program file, the second replacement $ 2, and push it according to the class. $ 0 is a special variable, its content is the file name of the current shell program, so $ 0 is not a location parameter, which does not include $ 0 when displaying all current location parameters.

(4) Predefined variables: The predefined variable and environment variable are similar, and the variables defined when the shell is started. It is different that users can only use these variables according to the definition of the shell, and cannot redefine it. All predefined variables are composed of $ characters and another symbol, and the common shell predefined variables are:

The number of position parameters.

$ * Content of all position parameters.

The $? Command is required to return the status.

$$ The current process process number.

$! The last process number running in the background.

$ 0 Currently executed process name.

Among them, $? Is used to check if the previous command is executed correctly. (In Linux, the command exit status is 0 indicates that the command is executed correctly, and any non-zero is indicated by the command error.)

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

(5) Variables for parameter replacement:

The shell provides a parameter replacement function so that the user can give different values ​​to different conditions. There are four variables for parameter replacement, which are usually associated with a certain positional parameter, and whether the value of the specified location parameter has set the value of the class determine the variable, their grammar and function are as follows.

a. Variable = $ {Parameters -word}: If the parameter is set, the value of the variable is placed with the value of the parameter, otherwise replaced with Word. That is, the value of this variable is equal to the value of a parameter, if the parameter is not set, the variable is equal to the value of Word.

b. Variable = $ {Parameters = Word}: If the parameter is set, use the value of the value of the parameter to set the value of the variable, otherwise set the variable to Word, and then replace the value of the parameters with Word. Note that the location parameters cannot be used in this way because the position parameters cannot be assigned to the position parameter in the shell program.

c. Variable = $ {parameter? Word}: If the parameters are set, the value of the variable is placed with the value of the parameter, otherwise it will display Word and exit from the shell, if Word is omitted, the standard information is displayed. This variable requires a value that is equal to a certain parameter. If this parameter is not set, you will display an information, then exit, so this approach is often used for an error indication.

d. Variable = $ {Parameters Word}: If the parameters are set, use the Word to replace the variable, otherwise it will not be replaced.

All "parameters" in these 4 forms can be either a location parameter, or another variable, but only the location parameters are used.

转载请注明原文地址:https://www.9cbs.com/read-106531.html

New Post(0)