Chapter VI Linux System Administration - redirection and pipeline technology

Chapter VI Linux System Administration

Redirection and piping technology

Outline

In the shell program, the most commonly used FD (file descriptor) about three, namely:

0 is a file descriptor, standard input (stdin)

1 is a file descriptor, standard output (stdout)

2 is a file descriptor, standard error (stderr)

A plurality Linux command appropriately combined together, make it work, so that we can more efficiently process data on a daily basis. To do this, we must engage in order to understand the principles of redirection of input and output redirection.

Briefly, input redirection refers to the import file to the command, and output redirection refers to the original data to be output to the screen information is written to the specified file. In the daily study and work, compared to the input redirection, we use a higher frequency output redirection, output redirection so in turn divided into two different techniques to redirect the standard output and error output redirection, as well as empty write and append write modes.

When to use redirection

1. When the screen output of information is important on it and we need to keep him down when

2. Background of the executive program ⾏, dry place that he does not want interference screen when normal output;

Example 3. ⾏ command system, for example, the timing results Perform the task, he can survive when desired;

4. Perform ⼀ some commands, we already know he error message may appear, when he discards want;

The error message when outputs need to correct information;

[root@RHCE7 ~]# find >/root/syj.txt 2>/root/syjerror.txt

 

 

 

 

6.1 Redirection Example

6.1.1 standard output redirection

[root@Redhat7 ~]# ifconfig >/root/syjjjj.txt 

Empty original file

6.1.2 standard output redirection (added)

[root@Redhat7 ~]# ifconfig >/root/syjjjj.txt 

In the original file to append

6.1.3 correct errors and redirect the output files are written to different

[root@Redhat7 etc]# find /etc 1>/root/syj.txt 2>/root/jjj.txt

6.1.4 redirected to the null device

[root@Redhat7 etc]# ls  /root  >ab  2>/dev/null

 

6.2 input redirection

Input redirection is <.

 

[root@Redhat7 ~]# grep root </etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

MySQL table structure input

[root@Redhat7 ~]# mysql -uroot -p123 <bbs.sql

 

 

6.3 using a redirect script

[root@Redhat7 ~]# cat ping.sh

ping -c1 192.168.27.140 &>/dev/null

if [ $? -eq 0 ]; then

echo "192.168.27.140 is up"

else

echo "192.168.69.113 is down"

be

[root@Redhat7 ~]# sh ping.sh

192.168.27.140 is up

6.4 Redirection summary

 

 

6.5 Process Pipeline Technology

Pipeline operator "|" symbol connecting the left and right commands, the output of the left standard commands, commands to the right of the standard input START.

 

6.5.1 Example pipeline technology - Sorting

[root@Redhat7 ~]# sort -t ":" -k3 -n /etc/passwd|head

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

Examples of techniques 6.5.2 Pipeline - CPU Statistics Top 5

[root@Redhat7 ~]# ps aux --sort=-%cpu | head -6 

USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

gdm        2443  0.3  4.1 1543032 77708 ?       Sl   21:45   0:02 gnome-shell --mode=gdm

root          1  0.1  0.2 123312  3880 ?        Ss   21:45   0:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 21

root          4  0.1  0.0      0     0 ?        S    21:45   0:00 [kworker/0:0]

root        281  0.1  0.0      0     0 ?        S    21:45   0:01 [kworker/1:1]

root          2  0.0  0.0      0     0 ?        S    21:45   0:00 [kthreadd]

6.5.3 Pipeline Technology practice tee

 

 

tee command to redirect data to a file, on the other hand may also provide a copy of the data as a subsequent redirection command stdin. Simply means that the data is redirected to the given file and screen.

Tee command content directly output as input to another command

 

 

 

[root@Redhat7 ~]# ls /iso7.2  | tee /output.txt | wc -l

15

[root@Redhat7 ~]# cat /output.txt                 

addons

EFI

EULA

GPL

images

isolinux

LiveOS

media.repo

Packages

release-notes

Repodt

RPM-GPG-KEY-redhat-beta

RPM-GPG-KEY-redhat-release

syjhct

TRANS.TBL

 

Tee and> the same effect, but the tee will be output to the screen

 

[root@Redhat7 ~]# date >syj.txt

[root@Redhat7 ~]# date | tee syj.txt

August 20, 2019 21:34:16 CST Tuesday

Published 37 original articles · won praise 0 · Views 2408

Guess you like

Origin blog.csdn.net/syjhct/article/details/100164510