Linux Enhancement Chapter 003-Pipe Character, Redirection and Environment Variables

Table of contents

Preface

1. Input and output redirection

2. Pipe command character

3. Command line wildcards

4. Commonly used escape characters

5. Important environment variables


Preface

If you understand the past, you can't admonish it, but if you know what's coming, you can pursue it. In fact, the lost path is not far away. I feel that what is now is but yesterday was not. The boat is sailing far away, the wind is blowing and the clothes are blowing. I asked Zheng Husband about the way forward, and I hated the faintness of the morning light.

I have put all the first edition into my selection. You can click on my avatar homepage and select the selection to watch. This series is based on the second edition of "Linux Should Be Learned" and is used to learn and practice with the book. The parts that are not smooth in the textbook are all shown to everyone in the simplest way, free and open source. You can Baidu the resources on your own. To learn, we need both dopamine and endorphins.

I have been extremely tired these two days, but I mustered up the courage to continue updating. It’s cold and I need to wear more clothes. I hope everyone will pay attention to their health. Why write about operation and maintenance? Because it is the basis of cloud computing and cloud servers. I feel like I am reminding myself and everyone not to think that things are as simple as you see. Sometimes you still need to look at the principles. By extension, those who are in school should also look ahead to the future - those who are thinking of crisis, those who are working still need to learn something - those who are thinking of quitting, those who are unemployed can also replenish themselves - those who are thinking of change, they will enter the melting pot of the workplace in the future. , in his position to seek his own government, and at the same time think about danger, retreat and change. The same song from the King of Heaven is given to everyone "Jay Chou - In the Name of the Father" . I hope everyone will continue to work hard and learn!

1. Input and output redirection

Input redirection refers to importing files into the command, while output redirection refers to writing the data information originally intended to be output to the screen into the specified file.

Standard input redirection (STDIN, file descriptor is 0) : input from the keyboard by default, but can also be input from other files or commands.

Standard output redirection (STDOUT, file descriptor is 1) : output to the screen by default.

Error output redirection (STDERR, file descriptor is 2) : output to the screen by default.

[root@bogon ~]# ll test2

-rw-r--r--. 1 root root 48 Nov  8 15:25 test2

[root@bogon ~]# ll test3

ls: cannot access 'test3': No such file or directory

Symbols and functions in input redirection

symbol effect
command<file Pass a file as standard input to a command
command<< delimiter Reads from standard input and does not stop until a delimiter is encountered.
command <file1>file2 Take file 1 as the standard input of the command and put the standard output to file 2

Symbols and functions in input redirection

symbol effect
Command > File Redirect standard output to a file (clear the data of the original file)
Command 2> File Redirect error output to a file (clear the data of the original file)
Command>>File Redirect standard output to a file (append to the original content)
Command 2>> File Redirect error output to a file (append to the original content)
command>>file2>&1 or command&>>file Write standard output and error output to a file (append to the original content)

In the standard output mode in the redirection, file descriptor 1 can be omitted, but file descriptor 2 in the error output mode must be written. Through standard output redirection, the information originally output by the man bash command to the screen is written to file bash.txt, and then display the contents of the bash.txt file:

[root@bogon ~]# man bash > bash.txt

[root@bogon ~]# head -n 3 bash.txt  

BASH(1)                                            General Commands Manual                                           BASH(1)

NAME

[root@bogon ~]#  

When writing data to a file in overwriting mode, the last written content will be overwritten each time, so there is only the last write result in the final file:

[root@bogon ~]# echo "i like linux" > bash.txt

[root@bogon ~]# head -n 1 bash.txt  

i like linux

[root@bogon ~]#  

Write data once to the bash.txt file through append writing mode:

[root@bogon ~]# head -n 2 bash.txt  

i like linux

i love linux

[root@bogon ~]#  

Standard output writes the information intended to be output to the screen to a file:

[root@bogon ~]# ll bash.txt 2> test2

-rw-r--r--. 1 root root 26 Nov 10 15:34 bash.txt

There is no distinction between standard output and error output. As long as the command has output information, all output information will be appended to the file:

[root@bogon ~]# ll bash.txt &>> test2

[root@bogon ~]# ll bash1.txt &>> test2

[root@bogon ~]# cat test2

-rw-r--r--. 1 root root 26 Nov 10 15:34 bash.txt

ls: cannot access 'bash1.txt': No such file or directory

Use input redirection to import the bash.txt file to the wc -l command, and count the number of content lines in the file (only read information flow data):

[root@bogon ~]# wc -l < bash.txt  

2

2. Pipe command character

The execution format is "Command A | Command B". The function of the pipe command symbol can also be summarized in one sentence as "the information originally output to the screen by the previous command is used as the standard input of the next command."

The command to find restricted login users is grep /sbin/nologin /etc/passwd and the command to count text lines is wc -l. Merge the two commands through a pipe:

[root@bogon ~]# grep /sbin/nologin /etc/passwd | wc -l

42

Use page turning to view the file list and attribute information in the /etc directory:

[root@bogon ~]# ll /etc/ | more

drwxr-xr-x.  2 root root        34 Nov  6 11:30 cupshelpers

drwxr-xr-x.  4 root root        78 Nov  6 11:27 dbus-1

drwxr-xr-x.  4 root root        31 Nov  6 11:28 dconf

drwxr-xr-x.  2 root root        33 Nov  6 11:36 default

drwxr-xr-x.  2 root root        40 Nov  6 11:29 depmod.d

--More--

Complete the password reset operation with one command:

[root@bogon ~]# echo "123" | passwd --stdin root

Changing password for user root.

passwd: all authentication tokens updated successfully.

ps, grep, and pipe characters are used together. Search for process information related to bash below:

[root@bogon ~]# ps aux | grep bash

root        1051  0.0  0.1  26244  2324 ?        S    15:02   0:00 /bin/bash /usr/sbin/ksmtuned

root        2346  0.0  0.3  27688  5708 pts/0    Ss   15:03   0:00 -bash

root        2964  0.0  0.0  12136  1148 pts/0    S+   15:58   0:00 grep --color=auto bash

Display all bash-related process information in the system and output it to the screen and file at the same time:

[root@bogon ~]# ps aux | grep bash | tee result.txt

root        1051  0.0  0.1  26244  2324 ?        S    15:02   0:00 /bin/bash /usr/sbin/ksmtuned

root        2346  0.0  0.3  27688  5708 pts/0    Ss   15:03   0:00 -bash

root        3056  0.0  0.0  12136  1036 pts/0    S+   16:05   0:00 grep --color=auto bash

[root@bogon ~]# cat result.txt  

root        1051  0.0  0.1  26244  2324 ?        S    15:02   0:00 /bin/bash /usr/sbin/ksmtuned

root        2346  0.0  0.3  27688  5708 pts/0    Ss   15:03   0:00 -bash

root        3056  0.0  0.0  12136  1036 pts/0    S+   16:05   0:00 grep --color=auto bash

3. Command line wildcards

Wildcards are universal symbols for matching information. For example, an asterisk (*) represents matching zero or more characters, a question mark (?) represents matching a single character, and a number [0-9] in square brackets represents matching 0 to 9. A single digit character between them, and the letter [abc] inside the square brackets means matching any one of the three characters a, b, and c.

Wildcards and their meanings in Linux systems

wildcard meaning
* any character
? A single arbitrary character
[a-z] single lowercase letter
[A-Z] single uppercase letter
[a-Z] single letter
[0-9] single number
[[:alpha:]] any letter
[[:upper:]] any uppercase letter
[[:lower:]] any lowercase letter
[[:digit:]] all numbers
[[:alnum:]] Any letters plus numbers
[[:point:]] Punctuation

Matches all files in the /dev directory that begin with nvm:

[root@bogon ~]# ll /dev/nvm*

crw-------. 1 root root 244, 0 Nov 10 15:02 /dev/nvme0

brw-rw----. 1 root disk 259, 0 Nov 10 15:02 /dev/nvme0n1

brw-rw----. 1 root disk 259, 1 Nov 10 15:02 /dev/nvme0n1p1

brw-rw----. 1 root disk 259, 2 Nov 10 15:02 /dev/nvme0n1p2

brw-rw----. 1 root disk 259, 3 Nov 10 15:02 /dev/nvme0n1p3

I only want to view information about files whose file names begin with nvme0n1p but are followed by a certain other character:

[root@bogon ~]# ll /dev/nvme0n1p?

brw-rw----. 1 root disk 259, 1 Nov 10 15:02 /dev/nvme0n1p1

brw-rw----. 1 root disk 259, 2 Nov 10 15:02 /dev/nvme0n1p2

brw-rw----. 1 root disk 259, 3 Nov 10 15:02 /dev/nvme0n1p3

Create files in batches:

[root@bogon ~]# touch {AA,BB,CC}.conf

[root@bogon ~]# ll *.conf

-rw-r--r--. 1 root root 0 Nov 10 16:17 AA.conf

-rw-r--r--. 1 root root 0 Nov 10 16:17 BB.conf

-rw-r--r--. 1 root root 0 Nov 10 16:17 CC.conf

4. Commonly used escape characters

The 4 most commonly used escape characters

Backslash (\) : Makes a variable after the backslash a simple character.

Single quotes (' ') : escape all variables in it to simple strings.

Double quotes (" ") : retain the variable attributes in them without escaping.

Backtick (` `) : Return the result after executing the command.

Define a variable named PRICE and assign it a value of 5, then output the string and variable information enclosed in double quotes:

[root@bogon ~]# PRICE=5

[root@bogon ~]# echo "price is $PRICE"

price is 5

Special symbols are extracted and escaped into plain text:

[root@bogon ~]# echo "Price is \$$PRICE"

Price is $5

Combine the backticks with the uname -a command and use the echo command to view the Linux version and kernel information of the machine:

[root@bogon ~]# echo `uname -a`

Linux bogon 4.18.0-348.el8.x86_64 #1 SMP Tue Oct 19 15:14:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

5. Important environment variables

Variables are data types used by computer systems to hold variable values. In Linux systems, variable names are generally in uppercase, and commands are in lowercase. The execution of commands in Linux is divided into four steps.

Step 1 : Determine whether the user enters the command (such as /bin/ls) in an absolute path or a relative path. If it is an absolute path, execute it directly. Otherwise, go to step 2 to continue the judgment.

Step 2 : The Linux system checks whether the command entered by the user is an "alias command", that is, replacing the original command name with a customized command name.

Use the alias command to create your own command alias. The syntax format is "alias alias=command"

To cancel a command alias, use the unalias command, the syntax format is "unalias alias"

Step 3: The Bash interpreter determines whether the user enters an internal command or an external command. Internal commands are instructions inside the interpreter and will be executed directly; while most of the time the user inputs external commands, which are handed over to step 4 for further processing.

You can use "type command name" to determine whether the command entered by the user is an internal command or an external command:

[root@bogon ~]# type echo

echo is a shell builtin

[root@bogon ~]# type uptime

uptime is /usr/bin/uptime

Step 4: The system searches for the command files entered by the user in multiple paths. The variable that defines these paths is called PATH. It can be simply understood as the "little assistant of the interpreter". Its function is to tell the Bash interpreter to wait. The location where the executed command may be stored, and then the Bash interpreter will obediently search these locations one by one. PATH is a variable composed of multiple path values. Each path value is separated by a colon. Adding and deleting these paths will affect the Bash interpreter's search for Linux commands.

[root@bogon ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@bogon ~]# PATH=$PATH:/root/bin

[root@bogon ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin

As a cautious and experienced operation and maintenance personnel, after taking over a Linux system, I will definitely check whether there are any suspicious directories in the PATH variable before executing the command.

The 10 most important environment variables

variable name effect
HOME The user's home directory (i.e. home directory)
SHELL The name of the shell interpreter the user is using
HISTSIZE Number of historical command records output
HISTFILESIZE Number of saved historical command records
MAIL Email save path
LANG System language, language family name
RANDOM Generate a random number
PS1 Bash interpreter prompt
PATH Define the path where the interpreter searches for commands executed by the user
EDITOR User default text editor

Check what values ​​the HOME variable has under different user identities:

[root@bogon ~]# echo $HOME

/root

[root@bogon ~]# su - centos8

Last login: Tue Nov  7 08:58:40 CST 2023 on pts/0

[centos8@bogon ~]$ echo $HOME

/home/centos8

Set a variable named WORKDIR to make it easier for users to enter a deeper directory:

root@bogon ~]# mkdir /home/workdir

[root@bogon ~]# WORKDIR=/home/workdir

[root@bogon ~]# cd $WORKDIR

[root@bogon workdir]# pwd

/home/workdir

Variables are not global and have limited scope. They cannot be used by other users by default:

[root@bogon workdir]# su centos8

[centos8@bogon workdir]$ cd $WORKDIR

[centos8@bogon ~]$ echo $WORKDIR

[centos8@bogon ~]$ exit

Use the export command to promote it to a global variable:

[root@bogon workdir]# export WORKDIR

[root@bogon workdir]# su centos8

[centos8@bogon workdir]$ cd $WORKDIR

[centos8@bogon workdir]$ pwd

/home/workdir

If you no longer use this variable, you can execute the unset command to cancel it:

[root@bogon workdir]# unset WORKDIR

[root@bogon workdir]#  

Variables set directly in the terminal can take effect immediately, but will become invalid after restarting the server. Therefore, we need to write the variables and variable values ​​​​in the .bashrc or .bash_profile file to ensure that they can be used permanently.

Guess you like

Origin blog.csdn.net/m0_68662723/article/details/134608317
Recommended