The Way to Learn Linux Operations and Maintenance (8) Pipeline and Redirection

Posted by shayman on Mon, 27 May 2019 00:26:10 +0200

I. File Descriptors
File Descriptor (fd): A file descriptor is a non-negative integer. When an existing file or a new file is opened, the kernel returns a file descriptor. Reading and writing a file also requires the use of a file descriptor to access the file. It is also the file record table that the kernel maintains for each process. File descriptors are only suitable for Unix and Linux operating systems.

II. Standard Input, Standard Output, Standard Error

3. Redirectional Symbols

Case 1: Output redirection (coverage)

# date > date.txt


You can see that the contents of the original file are overwritten.

Case 2: Output redirection (additional)

# echo "world" >> date.txt


You can see that the original file was not overwritten, but a line was added at the end.

Case 3: Error output redirection

# ls/nginx001/home > right.txt 2 > error.txt (coverage)

# ls/home/jack/root >>right.txt 2> error.txt (additional)

Case 4: Correct and incorrect output to the same location
One way

# ping -c1 -W1 192.168.1.103 &>file01.txt
# ls /home /king &>filefile02.txt

Mode two

# ls /hello /home >file03.txt 2>&1

Case 5: Redirecting to air equipment (/dev/null)
In the previous section on file management, we mentioned that the / dev/null empty device file, which acts like a space black hole, can redirect output to it without giving you any response.

# ping -c1 -W1 127.0.0.1
# ping -c1 -W1 127.0.0.1 &>/dev/null
# echo "hello" &>/dev/null

Case 6: Using redirection in scripts

# vim pingTest01.sh
ip= 192.168.144.132
ping -c1 -W1 $ip
if [ $? -eq 0 ];then
    echo "$ip is up."
else
    echo "$ip is down!"
fi
# bash pingTest01.sh

//Improvement, put unnecessary output into black hole (/ dev/null)
# vim pingTest02.sh
ip= 192.168.144.132
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
    echo "$ip is up."
else
    echo "$ip is down!"
fi
# bash pingTest02.sh

//Improvement, no output, save the results
# vim pingTest03.sh
ip= 192.168.144.132
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
    echo "$ip is up." >>up.txt
else
    echo "$ip is down!" >>down.txt
fi
# bash pingTest03.sh

Case 7: Use of input redirection
Example 1: Mail input redirection
1) Mail command: mail
Options:
- s Specifies the subject of the message
Example 1

# mail -s "object" 14625@qq.com 
            Theme Receiver

Example 2
Pipeline mail

# echo "hello $USER,welcome to Linux Mail\!" |mail -s "welcome" root
# mail
Order:
1 Check Mail 1
 d 1 Delete mail 1
 q Exit
 Help?

Example 3
Mail delivery using file input redirection

# echo "hello world!,I am Ccjedweat\!" >hello.txt
# mail -s "hello" root <hello.txt

Example 2: When Loop Read for Input Redirection
# vim redirectTest.sh
while read line; do
    echo "$line"
done </etc/passwd
# bash redirectTest.sh
Example 3: mysql table structure import
# mysql -uroot -p123 < user.sql
Example 4: Using redirection to output multiline files in scripts
# cat <<-EOF
1111111
222222
333333
EOF


Output file content until encountering EOF, <-EOF-will skip the space or tab key in the following content, omitting the effect of the space or tab key can not be skipped.

Example 5: Using redirection to create multi-line files
# cat >test.txt <<-EOF
 Why is it late to win and stick to it?
EOF

Extension point: subshell
Execute in the current shell

# cd /boot; ls
# pwd


You can see that the current working directory has changed

Execute in subshell

# cd
# (cd /boot; ls)
# pwd


You can see that the current working directory has not changed

If you do not want the execution of certain commands to have an impact on the current shell environment, execute in the subshell!

Four, pipeline
1. introduction
Pipeline is a basic IPC mechanism, which acts on the process of blood relationship and completes data transmission. It can take the output of the previous command as the input of the next command. In scripting, the use of pipelines is very common and very important. Pipelines have anonymous pipes and named pipes. This paper mainly introduces anonymous pipes.

2. Grammatical Structure
Process pipeline
Usage: command1 | command2 |command3 |

# ll /dev/ |file01
# ps aux |grep 'sshd'
# rpm -qa |grep 'httpd' //Query all installed packages and filter packages containing httpd
# yum list |grep 'httpd'
Case 1: Sort the users in / etc/passwd by UID size
 # sort -t":" -k3 -n /etc/passwd // / Separated by: Increase the third column by word number
 # sort-t ":" - k3-n/etc/passwd-r//reverse order
# sort -t":" -k3 -n /etc/passwd |head
 - t Specifies the field separator
 -k specified column
 - r reverse ordering
 -n by numerical value
Case 2: Five processes accounting for the most CPU
# ps aux --sort=-%cpu |head -5
Case 3: Statistics of shell types currently used by users in / etc/passwd
 Idea: Take out the seventh column (shell) | sort (same category) | de-duplicate
# awk -F":" '{print $7}' /etc/passwd
# awk -F":" '{print $7}' /etc/passwd |sort
# awk -F":" '{print $7}' /etc/passwd |sort |uniq 
# awk -F":" '{print $7}' /etc/passwd |sort |uniq -c
 - F Specifies the field separator
 $7 Seventh Field
Case 4: Statistical website visits top 20
 Idea: Print all accessed connections | Filter connections to access websites | Print users'IP | Sort | Deduplicate
# yum -y install httpd
# systemctl start httpd
# systemctl stop firewalld

# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c
# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20 
Case 5: Print all current IP
# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'

Case 6: Print the percentage of space used by the root partition (print only numbers)
# df -P |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'

3.tee pipeline
To record pipeline data of a part of the pipeline, you can add a tee pipeline after it. Its function is somewhat similar to output redirection. The tee pipeline can be regarded as the three branches of the tap pipe, which do not affect each other. That is to say, removing or adding tee pipeline has no effect on anonymous pipeline command. It is only used to receive and record the output of an anonymous pipeline.

# grep '^root' >user_info01.txt
# gerp '^root' |tee user_info02.txt
# ip addr |grep 'inet ' |tee ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
//Write the ip.txt file in an additional way
# ip addr |grep 'inet ' |tee -a ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
Here we leave you two questions, you can use all the tools around you to understand?
1. Understand the difference between anonymous pipes and named pipes?
2. How to create named pipes?

V. Parametric Transfer Xargs
Awk sed grep sort uniq less than xargs can follow pipeline commands directly, while ls cp rm can not follow pipeline commands directly, so it is necessary to pass parameters through xargs commands.

case1
[root@localhost ~]# vim files.txt 
/home/file1
/home/file2
/home/file3
/home/file4
/home/file5

[root@localhost ~]# cat files.txt |ls -l
[root@localhost ~]# cat files.txt |rm -rfv

[root@localhost ~]# cat files.txt |xargs ls -l
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file1
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file2
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file4
-rw-r--r--. 1 root root 0 Mar 11 10:35 /home/file5

[root@localhost ~]# cat files.txt |xargs rm -rvf 
removed '/home/file1'
removed '/home/file2'
removed '/home/file4'
removed '/home/file5'
Case 2
[root@localhost ~]# touch /home/file{1..5}
[root@localhost ~]# cat files.txt |xargs -I {} ls -l {}
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file1
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file2
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file4
-rw-r--r--. 1 root root 0 Mar 11 10:40 /home/file5

[root@localhost ~]# cat files.txt |xargs -I {} cp -rvf {} /tmp

[root@localhost ~]# cat files.txt |xargs -I CCjetweat cp -rvf CCjetweat /var/tmp
case3
[root@localhost ~]# find /etc -iname "*ifcfg*" |xargs -I {} cp -rf {} /tmp

Please support me a lot. There are some mistakes. Welcome to point out that it is not finished yet.

Topics: vim shell Linux MySQL