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

How to use the order symbol of Linux command execution

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the Linux command execution order symbol how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

To execute more than one command at a time, you can use & &, |, () to control the order in which the commands are executed.

I. & &

1. Format

Command1 & & command2

two。 Meaning

The command2 on the right of & & can only be executed after the command1 on the left is executed successfully (return 0 means successful).

3. Example

(1) when you successfully make a copy of the file sql.txt as sql.bak.txt, and then display the copy sql.bak.txt

[root@RHEL5 shell] # cp sql.txt sql.bak.txt & & cat sql.bak.txtDatabase Size (MB) Date Created----GOSOUTH 2244 12/11/97TRISUD 5632 8-9-99 (2 rows affected)

(2) when the file facebook.txt is sorted and saved in facebook.txt.sorted, the facebook.txt.sorted file is printed.

Sort facebook.txt > facebook.txt.sorted & & lp facebook.txt.sorted II. | |

1. Format

Command1 | | command2

two。 Meaning

If | | the command1 on the left fails (a return of 1 indicates failure), execute the & & command2 on the right.

3. Example

(1) print the first column of 1111.txt, and display the contents of facebook.txt if the execution is not successful.

[root@RHEL5 shell] # awk'{print $1} '1111.txt | | cat facebook.txt awk: cmd. Line:1: fatal: cannot open file `1111.txt' for reading (No such file or directory) google 110 5000baidu 100 5000guge 50 3000sohu 100 4500

(2) when the command to print the first column of 1111.txt is successfully executed, the command to print facebook.txt is not executed.

[root@RHEL5 shell] # awk'{print $1} 'facebook.txt | | cat facebook.txtgooglebaidugugesohu III.

If you want to execute several commands, you need to separate each command with a command separator semicolon and combine all commands with parentheses (). Combining | | and & & can achieve complex functions.

1. Format

(command1;command2;command3;...)

two。 Example

(1) use multiple commands. If the sort command is successful, back up the sorted files to the / root/backup/ directory before printing.

Sort facebook.txt > facebook.txt.sorted & & (cp facebook.txt.sorted / root/backup/facebook.txt.sorted;lp facebook.txt.sorted)

(2) if you fail to print the facebook.txt file, send an email to the root user and shut down the computer.

Lp facebook.txt.sorted | (echo "It was not submitted succuessfully" | mail root;init 0) Thank you for reading this article carefully. I hope the article "how to use the sequence symbol of Linux Command" shared by the editor will be helpful to everyone. At the same time, I also hope you will support it and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report