Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

3.2 Pipeline command


May 23, 2021 That's what Linux should learn



Careful readers will surely remember seeing something called a pipe character while studying the tr command in section 2.6. A t the same time, you can enter the pipe character by pressing the Shift key on the keyboard, which is executed in the |." C ommand B". T he role of the pipeline command can also be summed up in one sentence as "treating the standard normal data that the previous command was intended to output to the screen as a standard input to the 1st command". I n section 2.8, when explaining the grep text search command, we identified all users who were restricted from logging on to the system by matching keywords/sbin/nologin. At the end of this section, it is perfectly possible to combine the following two commands into one:

Find out if the command for the restricted login user is grep "/sbin/nologin" /etc/passwd;

The command that counts the number of lines of text is wc -l.

All you have to do now is pass the output value of the search command to the statistical command, that is, the list of user information that was intended to be output to the screen and then give it to the wc command for further processing, so you only need to put the pipe character between the two commands, as follows. It's so convenient!

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

This pipe character is like a magic weapon, we can apply it to other different commands, such as turning the page to view the file list and attribute information in the /etc directory (these contents will be displayed on the screen by default, not at all clear):

[root@linuxprobe ~]# ls -l /etc/ | m ore total 1400 drwxr-xr-x. 3 root root 97 Jul 10 17:26 abrt -rw-r--r--. 1 root root 16 Jul 10 17:36 adjtime -rw-r--r--. 1 root root 1518 Jun 7 2013 aliases -rw-r--r--. 1 root root 12288 Jul 10 09:38 aliases.db drwxr-xr-x. 2 root root 49 Jul 10 17:26 alsa drwxr-xr-x. 2 root root 4096 Jul 10 17:31 alternatives -rw-------. 1 root root 541 Jan 28 2017 anacrontab -rw-r--r--. 1 root root 55 Jan 29 2017 asound.conf -rw-r--r--. 1 root root 1 Jan 29 2017 at.deny drwxr-xr-x. 2 root root 31 Jul 10 17:27 at-spi2 drwxr-x---. 3 root root 41 Jul 10 17:26 audisp drwxr-x---. 3 root root 79 Jul 10 17:37 audit drwxr-xr-x. 4 root root 94 Jul 10 17:26 avahi --More--

When you modify a user's password, you typically need to enter two passwords to confirm it, which can be a fatal flaw when writing automated scripts. By combining the pipe character with the -stdin parameter of the passwd command, we can use one command to complete the password reset operation:

[root@linuxprobe ~]# echo "linuxprobe" | p asswd --stdin root Changing password for user root. passwd: all authentication tokens updated successfully.

Do you think the pipe character command is a little late to meet and hate? There are many ways to play pipe characters, for example, when sending e-mail messages, the default interactive way, we can use a combination of pipe characters of the command statement, the edited content with the title "packaging", and finally use this command to achieve the delivery of messages.

[root@linuxprobe ~]# echo "Content" | m ail -s "Subject" linuxprobe [root@linuxprobe ~]# su - linuxprobe Last login: Fri Jul 10 09:44:07 CST 2017 on :0 [ linuxprobe@linuxprobe ~]$ mail Heirloom Mail version 12.5 7/5/10. T ype ? f or help. "/var/spool/mail/linuxprobe": 1 message 1 new

N 1 root Sun Aug 30 17:33 18/578 "Subject"

If the reader is a Linux newman, you may find the command combination above very complicated, but the experienced reader will feel like a boot scratching addiction, they hope to write such convenient commands more advanced, more powerful. F or example, redirection technology can package multiple lines of information into or out at once, making daily work more efficient. In order to give everyone a good look at our book, Mr. Liu Wei is of course duty-thy-hand to hand over the technology.

The following self-made command uses a combination of the mail command and the input redirect demarcation, which is designed to allow the user to enter the content until the user enters its custom demarcation.

[root@linuxprobe ~]# mail -s "Readme" [email protected] << over

I think linux is very practical I hope to learn more can you teach me ? o ver [root@linuxprobe ~] #

Of course, don't mistake the pipeline command for using it only once in a combination of commands, and we can use it this way: "Command A |." C ommand B | C ommand C." T o help readers better understand the role of pipe characters, Mr. Liu often describes pipe characters as "arbitrary doors" during lectures. I 'm verse you all saw the A-Dream cartoon when you were a kid. A dream (also known as the machine cat) often in order to please the male and pull out a piece of treasure from the pocket, many times in which the use of any door this prop. In fact, the pipe character is like any door used to implement data crossing, can help us improve work efficiency, to complete the complex work that we could not have imagined before.