Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Semicolons & & and &, | and | | description and usage in linux

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

When using linux commands, we can execute multiple commands in one line or execute the next command conditionally. Let's explain the use of the semicolon & & and &, | and | | of the linux command.

Semicolon usage

Method: command1; command2

Separate each command with the; sign, and each command is executed in the order from left to right, regardless of whether it fails or not, all commands are executed.

"|" pipe symbol usage

The output of the previous command as the argument to the next command

Method: command1 | command2

The pipe character "|" provided by Linux separates the two commands, and the output of the command on the left side of the pipe character is used as the input of the command on the right side of the pipe character. Continuous use of pipes means that the output of the first command is used as the input of the second command, the output of the second command is used as the input of the third command, and so on

Using a pipe

# rpm-qa | grep licq

This command creates a pipe with a pipe character "|". The pipeline lists RPM packages with licq characters by taking the output of the rpm-qa command (including all installed RPM packages in the system) as input to the grep command.

Leverage multiple pipelin

# cat / etc/passwd | grep / bin/bash | wc-l

This command uses two pipes, using the first pipe to send the output of the cat command (showing the contents of the passwd file) to the grep command, the grep command to find out all the lines containing "/ bin / bash"; the second pipe sends the output of grep to the wc command, and the wc command counts the number of lines in the input. The function of this command is to find out how many users in the system use bash

The usage of "&" symbol

& put it after the startup parameter to set this process as a background process

Method: command1 &

By default, the process is the foreground process, so the Shell is occupied, and we cannot do other operations. For those processes that do not interact, we often want to start them in the background. We can add a'&'to the startup parameters to achieve this purpose.

The usage of "&" symbol

When shell executes a command, it returns a return value, which is saved in the shell variable $? Medium. When $? = = 0, the execution is successful; when $? = = 1 (I think it is a non-zero number, the return value is between 0 and 255), the execution failed.

Sometimes the next command depends on whether the previous command was executed successfully. For example, execute another command after the successful execution of one command, or execute another command after the execution of another command fails. Shell provides & & and | | to control the execution of commands. Shell will control the execution of subsequent commands based on the return value of the previous command.

The syntax format is as follows:

Command1 & & command2 [& & command3.]

The & & connection is used between commands to realize the function of logic and.

Only if the command on the left of & & returns true (the command return value $? = = 0), the command on the right of & & will be executed.

As long as a command returns false (the command returns a value of $? = = 1), the subsequent command will not be executed.

"| |" symbol usage

The function of logical OR

The syntax format is as follows:

Command1 | | command2 [| | command3.]

Use "|" to connect between commands to achieve the function of logic or.

Only if the command on the left returns false (the return value of the command $? = = 1), the command on the right will be executed. This is the same as the logic or syntax function in c language, that is, to implement short-circuit logic or operation.

As long as a command returns true (the command returns a value of $? = = 0), the subsequent command will not be executed. -stop execution until you return to the real place.

For example, the ping command determines the surviving host

Ping-c 1-w 1 192.168.1.1 & > / dev/null & & result=0 | | result=1

If ["$result" = = 0]; then

Echo "192.168.1.1 is UP!"

Else

Echo "192.168.2.1 is DOWN!"

Fi

Note that & > write together.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report