Linux system shell command tutorial

  
                

Shell in Linux is more than just a command, but an interpreter for other commands. You can debug other commands to complete the compilation. So how should the Shell command be used in Linux?

In fact, as the command command language interpretation and implementation of interactive user input is only one aspect of the Shell function, Shell can also be used for programming, it provides the means to define variables and parameters as well as Program control structure. Using Shell programming is similar to a batch file in DOS, called a shell script, also known as a shell program or a shell command file.

Shell Basic Syntax

Like the high-level programming language, Shell also provides the ability to explain and use variables. For the shell, the value of all variables is a string, the shell program uses the form of $var to refer to the value of the variable named var.

Shell has the following basic types of variables.

(1) Shell-defined environment variables:

The shell has defined some variables related to the working environment of the system at the beginning of execution, users can also redefine these variables, commonly used The shell environment variables are:

HOME The full path name used to save the registration directory.

PATH is used to save directory names separated by colons. The shell will search for these directories in the order given in the PATH variable, and the first executable found with the command name will be executed.

Type of TERM terminal.

UID The identifier of the current user. The value is a string 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 main prompt, under privileged users, the default primary prompt is #, under normal users, the default primary prompt is $.

PS2 In the process of receiving the user input command from the shell, if the user enters “\\” at the end of the input line, then press Enter, or when the user presses the Enter key, the Shell determines that the command entered by the user is not At the end, this secondary prompt is displayed, prompting the user to continue entering the rest of the command. The default secondary prompt is "."

(2) User-defined variables:

Users can define their own variables according to the following syntax rules:

Variable Name=Variable Value

The point to note is that when defining a variable, the variable name should not be preceded by the symbol $. When referring to the content of the variable, the variable name should be preceded by $; when assigning a value to the variable, the equals must not leave spaces on both sides, if the variable itself If a space is included, the entire string is enclosed in double quotes.

When writing a shell program, in order to distinguish variable names from command names, it is recommended that all variable names be represented in uppercase letters.

Sometimes we want to specify a variable and set it to a specific value and then not change its value, you can use the following command to ensure the readability of a variable:

readonly variable name

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

export variable name

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

export variable name=variable value< Br>

Use the variables described by export to be accessible in all commands or programs that are run after the shell.

(3) Positional parameters:

Positional parameters are variables that are determined according to their respective positions on the command line that calls the shell program, and are parameters that are entered after the program name. The location parameters are separated by spaces. The shell takes the first position parameter to replace $1 in the program file, the second replaces $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 positional parameter. It does not include $0 when displaying all current positional parameters.

(4) Predefined variables:

Predefined variables are similar to environment variables, and are defined at the beginning of the shell. The difference is that users can only use these variables according to the definition of the shell, and can not redefine it. All predefined variables are composed of a $ character and another symbol. Commonly used shell predefined variables are:

$# The number of positional parameters.

$* The contents of all location parameters.

$? The status returned after the command is executed.

$$ The process ID of the current process.

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

$0 The name of the currently executing process.

Where, $? Used to check if the previous command was executed correctly. (In Linux, a command exit status of 0 indicates that the command is executed correctly, and any non-zero value indicates a command error.)

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

(5) Variables for parameter substitution:

The Shell provides a parameter substitution function so that users can assign different values ​​to variables according to different conditions. There are four kinds of variables for parameter substitution. These variables are usually associated with a certain position parameter. The value of the variable is determined according to whether the specified position parameter has been set. Their syntax and functions are as follows.

a. Variable = ${parameter-word}: If the parameter is set, replace the value of the variable with the value of the parameter, otherwise replace it 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 = ${parameter = word}: If the parameter is set, replace the value of the variable with the value of the parameter, otherwise set the variable to word and then replace the value of the parameter with word. Note that positional parameters cannot be used in this way because positional parameters cannot be assigned values ​​in the Shell program.

c. Variable = ${ parameter? Word}: If the parameter is set, the value of the variable is replaced with the value of the parameter, otherwise the word is displayed and exited from the shell. If the word is omitted, the standard information is displayed. This variable requirement must be equal to the value of a certain parameter. If this parameter is not set, a message is displayed and then exits, so this method is often used for error indication.

d. Variable = ${parameter + word}: If the parameter is set, the variable is replaced with word, otherwise the substitution is not performed.

The "parameters" in all four forms can be either positional parameters or another variable, but only with positional parameters.

The above is the tutorial for using the Shell command. It can be said that the Shell command is an indispensable programming tool in the Linux system.

Copyright © Windows knowledge All Rights Reserved