Shell学习 - 1.15 如何同时执行多个命令

Linux 终端是一个功能强大的工具,允许你使用命令执行各种系统操作。文件操作、程序管理和服务自动化是使用 shell 命令可以高效执行的部分操作。

The Linux terminal is a powerful tool that allows you to perform various system operations using commands. File manipulation, program management, and service automation are some of the operations you can carry out efficiently using shell commands.

不过,在执行多个操作时,一条一条地运行命令并不高效。更快捷的方法是将多条命令串联在一行中。这不仅能加快处理速度,还能节省时间。

However, when it comes to executing multiple operations, running commands one by one isn't efficient. A faster way to do it is to chain multiple commands in one line. Not only does this speed up the process, but it also saves you time.

了解如何在 Linux 中同时执行多个命令可以大大提高你的效率和生产力。同时执行多个 Linux 命令,更好地控制系统(和时间)。本文将引导你了解在一行中运行多个 Linux 命令的各种方法,甚至是如何自动执行重复性任务。

Understanding how to execute multiple commands at once in Linux can significantly improve your efficiency and productivity.  Take better control of your system (and time) by executing multiple Linux commands at once. This article will guide you through various ways you can run multiple Linux commands in a single line and even how to automate repetitive tasks.

Understanding the Basics [先了解一些基础知识]

在深入学习高级技术之前,你应该先熟悉 Linux 的强大工具--命令行或终端。在这里,你可以通过键入一系列命令来执行任务。虽然一开始可能会让人望而生畏,但学会使用它可以为你打开一个提高效率和生产力的新世界。

Before delving into advanced techniques, you should familiarize yourself with the command line or Terminal, Linux's powerful tool. Here, you can perform tasks by typing a sequence of commands. While it may seem daunting at first, learning to use it can open up a new world of efficiency and productivity.

Running Commands Consecutively [连续运行多个命令]

如果要连续运行多条命令,即在前一条命令结束后运行下一条命令,请使用分号(;)。例如,command1 ; command2 ; command3 将执行命令 1,等待命令 1 结束后再执行命令 2,以此类推。

不管前一条命令执行失败或成功,或者输出情况如何,都会按顺序继续执行下一条命令。

If you want to run multiple commands consecutively, i.e., run the next command after the previous one finishes, use the semicolon (;). For instance, command1 ; command2 ; command3 will execute command1, wait for it to finish, and then execute command2 and so on.

比如,查看当前用户和系统的主机名:

$ whoami; hostname

snow

ubuntu-vm

Executing Commands in Parallel [并行执行多个命令]

要同时或并行运行命令,请使用与操作符 (&)。但请注意,使用"&"会将进程发送到后台,使下一条命令立即启动。例如,command1 & command2 会同时执行 command1 和 command2。

To run commands simultaneously or in parallel, use the ampersand (&). However, keep in mind that using an ampersand sends the process to the background, allowing the next command to start immediately. For instance, command1 & command2 executes both command1 and command2 at the same time.

Using the Logical Operators [使用逻辑运算符]

您还可以使用逻辑运算符(&& 和 ||),根据前一条命令的成功或失败执行命令。“&&"操作符会在前一条命令成功的情况下执行下一条命令。例如,command1 && command2 只有在command1 成功执行的情况下才会执行command2。相反,"||"操作符只有在前一条命令失败时才会执行下一条命令。

You can also employ logical operators (&& and ||) to run commands based on the success or failure of the previous command. The '&&' operator will execute the next command if the previous one succeeds. For instance, command1 && command2 will only execute command2 if command1 is successful. Conversely, the '||' operator will execute the next command only if the previous one fails.

举例:比如如果找不到Document.txt则创建一个新文件:

$ find . -name Document.txt || touch Document.txt

创建文件夹,成功后进入此路径:

$ mkdir Documents && cd Documents

也可以组合使用逻辑运算符,在command A执行失败后,才执行command B和C。

$ command A || command B && command C

举例:

find . -name Document || echo "Directory not found" && mkdir Document

如果目录不存在,终端会将控制流转到 echo 和 mkdir 命令,这两条命令分别打印指定字符串和创建新文件夹。

If the directory isn't present, the terminal transfers the flow to the echo and mkdir commands, which print the specified string and create a new folder, respectively.

此外,您还可以使用布尔表达式 false 和 true,它们分别被 shell 解释为 1 和 0。

Also you may use boolean expressions false and true which are respectively interpreted by the shell as the values 1 and 0.

$ true && echo "hello world"

hello world

命令执行成功,以及使用true,实际的值状态都是0。

$ echo $((true))

0

Grouping Commands [ 组命令 ]

如果您想按特定顺序执行一组命令,可以使用括号。例如,(command1 ; command2) & command3 将同时运行 command1 和 command3,但只有在1、3两个命令都执行完毕后才启动 command2。

If you have a group of commands that you want to execute in a specific order, you can use parentheses. For example, (command1 ; command2) & command3 will run command1 and command3 simultaneously but will only initiate command2 once command1 have completed.

Utilizing Command Line Pipes [ 利用命令行管道 ]

当你想将一条命令的输出作为另一条命令的输入时,管道是一个非常有用的工具。使用竖杠 (|) 就可以实现这一目的。例如,command1 | command2 将把命令 1 的输出作为命令 2 的输入。

Pipes are an invaluable tool when you want to pass the output of one command as the input to another. You can do this by using the vertical bar (|). For instance, command1 | command2 would pass the output of command1 as input to command2.

Automating Repetitive Tasks [ 重复性任务自动化 ]

如果你经常执行一组特定的命令,你可以编写一个简单的 bash 脚本来自动执行该过程。你只需将命令写入文本文件,并以 .sh 扩展名保存即可。例如,你可以创建一个名为 "myscript.sh "的文件并写入以下内容:

If you frequently execute a particular set of commands, you can write a simple bash script to automate the process. All you have to do is write the commands in a text file and save it with a .sh extension. For example, you can create a file named 'myscript.sh' and write:

#!/bin/bash

command1

command2

command3

然后,运行 chmod +x myscript.sh 使脚本可执行,并使用 ./myscript.sh 执行。

Then, run chmod +x myscript.sh to make the script executable and execute it with ./myscript.sh.

比如执行系统升级:

#!/bin/bash

sudo apt update && sudo apt upgrade -y

In Conclusion [ 总结 ]

上述操作的目的和语法是不同的。同时可以灵活组合多种方式。

掌握同时执行多条 Linux 命令的技巧可以节省大量时间,并极大地改进你的工作流程。通过了解分号、&、逻辑运算符、括号、管道和 bash 脚本,你将有能力让终端以更高效、更强大的方式为你工作。

The purpose and the syntax of the above operations are different.  And it's OK to combine them altogether.

Mastering the art of executing multiple Linux commands at once can be a real time-saver and greatly enhance your workflow. By understanding the semicolon, ampersand, logical operators, parentheses, pipes, and bash scripting, you'll have the ability to make the terminal work for you in more efficient and powerful ways.

参考:

1,Linux Journal

Running Multiple Linux Commands Simultaneously | Linux Journal

2,MakeUseOf

How to Run Multiple Commands in Linux at Once

3,Medium

https://medium.com/@rldsn/learning-bash-part-2-sequential-and-conditional-execution-7a23bca050ba

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值