Linux---Shell (using skills and variables)

Table of contents

1. Concept

Second, the use of Shell skills

2.1 Automatic completion

2.2 Command Substitution

2.2.1 Backticks (``)

2.2.2 Money symbol $()

2.3 I/O heavy orientation

2.3.1 Standard input

 2.3.2 Standard output

2.3.3 Standard Error

2.4 Pipeline

 3. Variables in Shell

3.1 Local variables

3.2 Environment variables

3.2.1 Definition and clearing of environment variables

3.2.2 Several important environment variables

3.2.3 Configuration file of environment variables

3.3 Position variables

3.4 Special variables


1. Concept

        The shell is a program with special functions, which is located between the user and the kernel, and provides an interface for the user to interact with the kernel; the shell can receive commands entered by the user, and send the commands to the kernel for execution. After the kernel receives the user's command Scheduling hardware resources to complete the operation, and then return the result to the user.

Simply put: program instructions ---> kernel ---> hardware resources

Second, the use of Shell skills

2.1 Automatic completion

        Use the tab key to automatically search for matching commands, files, directories, etc. according to the input string. If the matching result is unique, the Shell will automatically complete it; if there are multiple matching names, the Shell will list them after pressing the tab key twice. All matches.

For example:

cat ./ View the files under the home path

2.2 Command Substitution

2.2.1 Backticks (``)

       The function is to replace the command character with the execution result of the command

Such as: echo `ls` (replace the ls command, output the files and directories in the current directory)

2.2.2 Money symbol $()

        Works the same as backticks

For example: echo $(ls)

2.3 I/O heavy orientation

2.3.1 Standard input

        The descriptor of the standard input file is 0, the default device is the keyboard, and the command reads the required data from the standard input file when it is executed

For example: input redirection "< " can specify the input whose rvalue is lvalue, the specific format is: command 0 < file name

where 0 is the standard input file identifier and can be omitted

Such as: wc -l < ​​./lisi/1.txt (use the content in 1.txt as the input of wc, count the number of lines in the file)

 2.3.2 Standard output

        The descriptor of the standard output file is 1, and the default device is the monitor. After the command is executed, the output result will be sent to the standard output file.

Such as: output redirection " > " can use the rvalue as the output of the lvalue, the specific format is: command 1 > file name

where 1 is the standard input file identifier and can be omitted

Such as: cat ./lisi/1.txt > 2.txt (output the execution result to the file 2.txt by overwriting)

The ">>" operator can be used to append the results to the file file

2.3.3 Standard Error

        The descriptor of the standard error file is 2, the default device is the monitor, and the error information generated by the command will be sent to the standard error file.

Such as: error redirection " > ", the specific command format is: command 2 > file name

Where 2 is the standard input file identifier, can not be omitted, can not have spaces

Such as: cat 3.txt 2> 4.txt (open the non-existing file 3.txt, and redirect the execution error message to the file 4.txt)

After executing this command, no error message will be output on the screen, the error message is in newfile, and you can also use ">>" to append

2.4 Pipeline

        The pipe symbol " | " can connect multiple simple commands, so that the output of one command can be used as the input of another command to complete complex functions. The format is: command 1 | command 2 | ... | command n

Such as: ls -l /lisi | grep 1.txt (output the line containing the 1.txt keyword in the file information in the etc directory)

 

 3. Variables in Shell

3.1 Local variables

        Local variables are equivalent to local variables in C language and are only valid in this shell. If the shell exits, the local variables will be destroyed. The format is: NAME = value

NAME is the variable name, and value is the value assigned to the variable. If value is not specified, the variable will be assigned an empty string. When using a variable, add a $ symbol before the variable. Shell also supports the output of multiple variables. Variables and No spaces between values

For example: NAME=tom, AGE=18 

        echo my name is $NAME and $AGE

You can also use the read command to read variable values ​​from the standard input, and the -p item of read can set the input prompt information

如: read -p "please you input an int number:" a

To delete the defined variable, you can use the unset command

Such as: unset NAME 

  

3.2 Environment variables

        Environment variables are used to initialize the shell's startup environment

3.2.1 Definition and clearing of environment variables

        Generally used to store a list of paths, these paths are used to search for executable files, library files, etc., format: export ENVIRPON-VARIABLE= value

Environment variables must be exported using export, the role of the export keyword is to declare this variable as an environment variable

Such as: export APPSPATH=/usr/local (define the APPSPATH variable and assign it to /usr/local)

The environment variables defined using export in the command line are only valid in the current shell and sub-shells, and these environment variables will be lost after the shell is restarted.

        Use the env command to view all environment variables, including user-defined environment variables

        Deleting environment variables is the same as deleting local variables, calling the unset command

Such as: unset APPSPATH 

3.2.2 Several important environment variables

(1) PATH

Used to help the Shell find the command entered by the user, PATH records the series of directories of the executable program entered by the user

Such as: $PATH

 The value of PATH can be modified, but you cannot directly assign a new value when modifying, otherwise the existing value of PATH will be overwritten. If you want to add a new directory to PATH, you can use:

PATH=$PATH:/new directory

$PATH represents the original PATH variable, and new directory represents the new path to be added

(2) PWD and OLDPWD

$pwd records the current directory path. When using cd to switch to other directories, the system automatically updates the value of $pwd, and OLDPWD saves the old working directory

(3) HOME

Record the current user's home directory

(4) SHELL

The variable of SHELL is /bin/bash, indicating that the current shell is bash. If it is necessary to use other shells, you need to reset the value of the SHELL variable

(5) USER and UID

An environment variable used to save user information, USER saves the name of the logged-in user, UID saves the ID of the logged-in user

(6) PS1 and PS2

PS1 and PS2 are called prompt variables, which are used to set the prompt format;

PS1 is used to set the first-level Shell prompt, also known as the main prompt

\u: Indicates the current user name

\h: Indicates the host name

\w: Indicates the current directory name

\$ is the directory prompt, the normal user is the $ symbol, root is the # symbol

PS2 is used to set the secondary shell prompt

The value of PS2 is > , when the input command is incomplete, a secondary prompt will appear

3.2.3 Configuration file of environment variables

Environment variables in Linux include system-level and user-level . System-level environment variables are valid for every user, and user-level environment variables are only valid for the current user;

The configuration files of environment variables are also divided into system level and user level . System level files include /etc/profile, /etc/profile.c, /etc/bashrc, /etc/environment, etc. These files are permanently valid for all users ;

User-level environment variable configuration files are mainly two files, .bash_profile and .bashrc, located in the home directory

The .bash_profile file mainly defines the environment variables of the current shell, and the .bashrc file is mainly used to define the sub-shell environment variables

The above-mentioned files can define permanent and effective environment variables, but it is necessary to distinguish the range of environment variables

3.3 Position variables

        Mainly used to introduce the parameters passed into the shell script, also known as positional parameters, the name consists of "$" and an integer: $n

For example, the variable $1 receives the first parameter passed into the script. When the integer in the current variable name is greater than 9, it needs to be enclosed with {}, such as ${11}. The position variable is the only variable in the shell that uses numbers to name all variables. , it should be noted that n starts from 1, and $0 represents the script itself

3.4 Special variables

Special variables commonly used in the shell:

$#: Parameters passed to the script

$* and $@ : all arguments passed to the script

$?: Command exit status, 0 means normal exit, non-zero means abnormal exit

$$: indicates the PID of the process

Guess you like

Origin blog.csdn.net/weixin_64428129/article/details/127343717