Linux bash pipe character "|" usage introduction and example

In the Linux system, the functions of bash are very rich and powerful.

We can program in bash, loop, judge, and write results to files.

All system settings can be inquired and changed.

Can write batch orders, timed plans.

You can even write games and play games.

The pipe character we introduced today is a magical tool that can easily connect two unrelated programs, hand over the results of one program to the other for processing, and even hand over the processing continuously.

First, let's talk about a usage that is used a lot.

ps –ef |grep  python
  • 1
  • 2

Ps is a very powerful process viewing tool in linux, where -e is to display all processes, and -f is to display in full format.

Grep is a very efficient query tool that can query the lines with a certain keyword in the text.

The function of this command is to query the process with the keyword python. will be listed one by one.

Using ps -ef alone will list all processes, but we don't need to see all processes, we just need to see if the process we want exists in the background, so use the pipe character to pass all process information to grep program to help us search for the process information we want. It is simply an artifact. For example, if we want to check whether the ssh service is enabled, we can runps –ef |grep ssh

Here are a few questions that can help us quickly understand other magical uses of the pipe character. Let's do it together.

1. Convert the content in the /etc/issue file to uppercase and save it to the /tmp/issue.out file

There are several key points in this question. The first is to obtain the content of the issue file. It is very simple. We use the cat /etc/issuecommand

(Issue file saves the prompt information before system login), followed by conversion to uppercase, we use the conversion command tr to achieve, tr [az] [AZ]. Finally, save it to a file with redirection.

So the command for this question is:

cat /etc/issue |tr [a-z] [A-Z] > /tmp/issue.out
  • 1
  • 2

2. Convert the information of the current system login user to uppercase and save it to the /tmp/who.out file

This question is the same. We know the last two functions. To get the information of the current user, we use the who command.

who | tr 'a-z' 'A-Z' > /tmp/who.out
  • 1
  • 2

3. A linux user sends an email to root, requesting that the title of the email be help, and the body of the email is as follows:

Hello, I am 用户名,The system version is here,please help me to check it ,thanks!

操作系统版本信息
  • 1
  • 2
  • 3
  • 4

There are several points in this question, the username should be replaced, the final operating system version information should also be replaced, and there is a carriage return. We use a command, echo is to output the information behind echo.

as output usernameecho $USER

Output operating system version informationuname –a

The carriage return is simple\n

Let's put together how to write the content of an email:

echo -e 'Hello, I am ' $USER ',The system version is here,please help me to check it,thanks!\n' `uname -a`
  • 1
  • 2

(The red line is the command, the bottom is the result)

echo -e will interpret the following \n, otherwise it will only be treated as a character. Characters should be enclosed in single quotes to avoid problems. $USER cannot be quoted, \n must be in quotes. Uname -a is a command, so we add backticks.

Next is the command to send the email

The format is like this mail -s "mail header" recipient content

We use the pipe character to pass in the content

echo -e 'Hello, I am '$USER ',The system version is here,please help me to check it,thanks!\n' `uname -a`|mail -s "help" root
  • 1
  • 2

write picture description here
4. Calculate the sum of 1+2+3+..+99+100

This thread is very classic.

When you see this question, what do you want to do? Write a loop i++ j++? Or use the formula (1+100)*n/2?

Our bash can solve this problem very neatly.

First of all, we need to know that there is a calculator in linux called bc, let's take a look at its usage first.

  1. 1 Enter bc directly to enter the program

  2. 2 throw an equation to bc

Seeing the second usage, we may realize that bc can directly calculate the string input to him!

Isn't it fun?

So can we get a string of 1+2+3+..+99+100?

We know that echo {1..100} can generate a string of 1 2 3 4 5 ... 100, separated by spaces.

Wait.. separated by spaces? Wouldn't it be okay to replace the space with a plus sign? Let's:

Echo {1..100} |tr ' ' '+'
  • 1
  • 2

See what we got?

come againEcho {1..100} |tr ' ' '+'|bc 
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325549193&siteId=291194637