Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the use of common functions of Shell commands in Linux system

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "introduction to the use of common functions of Shell commands in Linux system". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "Linux system Shell commands commonly used functions of the introduction" it!

In Linux system, Shell is not only a command, but also an interpreter of other commands. You can debug other commands to complete the compilation. So how should the Shell command be used in a Linux system?

In fact, as a command language to interactively interpret and execute commands entered by users is only one aspect of the function of Shell, Shell can also be used for programming, it provides the means to define variables and parameters and rich program control structure. Programming with Shell is similar to a batch file in DOS, called Shell script, also known as a Shell program or Shell command file.

Basic syntax of Shell

Like high-level programming languages, Shell also provides the ability to specify and use variables. For Shell, the values of all variables are a string, and the Shell program refers to 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:

Shell has defined some variables related to the working environment of the system at the beginning of execution, and users can also redefine these variables. Common Shell environment variables are:

HOME is used to save the full pathname of the registration directory.

PATH is used to hold directory pathnames separated by colons. Shell will search these directories in the order given in the PATH variable, and the first executable file found that matches the command name will be executed.

The type of TERM terminal.

The identification word of the current user of UID. The value is a string composed of digits.

The absolute pathname of the PWD's current working directory, and the value of this variable varies with the use of the cd command.

PS1 main prompt, under privileged users, the default main prompt is #, and for ordinary users, the default main prompt is $.

PS2 in the process of receiving a user input command by Shell, if the user enters "\" at the end of the input line and then enter, or when the user presses the enter key and Shell determines that the command entered by the user has not ended, it displays this auxiliary prompt, prompting the user to continue to enter the rest of the command. The default auxiliary prompt is ".

(2) user-defined variables:

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

Variable name = variable value

It should be noted that when defining a variable, the variable name should not be preceded by the symbol $, and when referencing the contents of the variable, the variable name should be preceded by $; when assigning values to the variable, there must be no spaces on both sides of the equal sign, and if the variable itself contains spaces, the entire string should be enclosed in double quotation marks.

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

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

Readonly variable name

At any time, the variable created is only a local variable of the current Shell, so it cannot be utilized by other commands or Shell programs run by Shell, while the export command can provide a local variable to other commands executed by Shell in the following format:

Export variable name

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

Export variable name = variable value

Using the variables described by export, you can access them in all commands or programs that run after Shell.

(3) position parameters:

Position parameters are variables that are determined according to their respective positions on the command line that calls the Shell program, and are entered after the program name. The position parameters are separated by spaces, and Shell takes the first position parameter to replace $1 in the program file, the second to replace $2, and so on. $0 is a special variable whose content is the file name of the current Shell program, so $0 is not a location parameter and does not include $0 when displaying all current location parameters.

(4) predefined variables:

Predefined variables are similar to environment variables and are defined at the beginning of Shell. The difference is that users can only use these variables according to the definition of Shell, not redefine it. All predefined variables are made up of the $sign and another symbol. The commonly used Shell predefined variables are:

$# the number of location parameters.

$* the contents of all location parameters.

$? The state returned after the command is executed.

The process number of the current process.

$! The last process number running in the background.

$0 the name of the currently executing process.

Of which, $? Used to check whether the previous command was executed correctly. (in Linux, a command exit status of 0 indicates that the command is executed correctly, and any value other than 0 indicates that the command is in error. )

The most common use of the $variable is to use the name of the staging file to ensure that the staging file is not duplicated.

(5) variable of parameter replacement:

Shell provides a parameter replacement function so that users can assign different values to variables according to different conditions. There are four kinds of parameter replacement variables, these variables are usually associated with a location parameter, according to whether the specified location parameter has been set to determine the value of the variable, their syntax and functions are as follows.

a. Variable = ${parameter-word}: if the parameter is set, the value of the variable is replaced with the value of the parameter, otherwise it is replaced with word. That is, the value of this variable is equal to the value of a parameter, and 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 they cannot be assigned values in Shell programs.

c. Variable = ${Parameter? Word}: if the parameter is set, the value of the variable is replaced with the value of the parameter, otherwise word is displayed and exited from the Shell, and if word is omitted, standard information is displayed. Such a variable must be equal to the value of a parameter. If this parameter is not set, a message is displayed and then exited, so this method is often used to indicate an error.

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

The "parameter" in all four forms can be either a position parameter or another variable, but it is more common to use a position parameter.

At this point, I believe you have a deeper understanding of the "introduction to the common functions of Shell commands in the Linux system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report