![linux中一个页表有多大](https://i-blog.csdnimg.cn/blog_migrate/70f17e59a697a3dde8fc1b3f24cd54b9.png)
linux中一个页表有多大
![00_lead_image_combining_commands](https://i-blog.csdnimg.cn/blog_migrate/69459dc729929a7cd7cf5eb7bfde79bb.png)
If you use Linux, you know how useful the command line can be for working with files, installing software, and launching programs. But it can be even more efficient if you run multiple commands at once.
如果您使用Linux,那么您将知道命令行在处理文件,安装软件和启动程序方面可能很有用。 但是,如果一次运行多个命令,则效率可能更高。
Combining two or more commands on the command line is also known as “command chaining”. We’ll show you different ways you can combine commands on the command line.
在命令行上组合两个或多个命令也称为“命令链接”。 我们将向您展示在命令行上组合命令的不同方式。
选项一:分号(;)运算符 (Option One: The Semicolon (;) Operator)
The semicolon (;) operator allows you to execute multiple commands in succession, regardless of whether each previous command succeeds. For example, open a Terminal window (Ctrl+Alt+T in Ubuntu and Linux Mint). Then, type the following three commands on one line, separated by semicolons, and press Enter. This will give you a listing of the current directory ( ls
), find out which directory you’re currently in ( pwd
), and display your login name ( whoami
) all at once.
分号(;)运算符使您可以连续执行多个命令,而不管每个先前的命令是否成功。 例如,打开一个终端窗口(在Ubuntu和Linux Mint中为Ctrl + Alt + T)。 然后,在一行上键入以下三个命令,以分号分隔,然后按Enter。 这将为您提供当前目录的列表( ls
),找出您当前所在的目录( pwd
),并一次显示您的登录名( whoami
)。
ls ; pwd ; whoami
You don’t have to put spaces between the semicolons and the commands, either. You can enter the three commands as ls;pwd;whoami
. However, spaces make the combined command more readable, which is especially useful if you’re putting a combined command into a shell script.
您也不必在分号和命令之间放置空格。 您可以输入以下三个命令: ls;pwd;whoami
。 但是,空格使组合后的命令更具可读性,如果将组合后的命令放入shell脚本中 ,则这特别有用。
![01_combining_commands_with_semicolon_operator](https://i-blog.csdnimg.cn/blog_migrate/9ab6e6550e421d7dbd3b06fd39df4e76.png)
选项二:逻辑AND运算符(&&) (Option Two: The Logical AND Operator (&&))
If you want the second command to only run if the first command is successful, separate the commands with the logical AND operator, which is two ampersands ( &&
). For example, we want to make a directory called MyFolder and then change to that directory–provided it was successfully created. So, we type the following on the command line and press Enter.
如果你想,如果第一个命令是成功的第二个命令只运行,与逻辑运算符,这是两个符号(分隔命令&&
)。 例如,我们要创建一个名为MyFolder的目录,然后更改为该目录(前提是已成功创建该目录)。 因此,我们在命令行上键入以下内容,然后按Enter。
mkdir MyFolder && cd MyFolder
The folder was successfully created, so the cd
command was executed and we are now in the new folder.
该文件夹已成功创建,因此执行了cd
命令,我们现在位于新文件夹中。
We recommend using the logical AND operator rather than the semicolon operator most of the time (;
). This ensures that you don’t do anything disastrous. For example, if you run a command to change to a directory and then force remove everything in that directory recursively ( cd /some_directory ; rm -Rf *
), you could end up ruining your system if the directory change didn’t happen. Not that we recommend you run a command to unconditionally remove all files in a directory at once.
我们建议在大多数情况下( ;
)使用逻辑AND运算符,而不要使用分号运算符。 这样可以确保您不会做任何灾难性的事情。 例如,如果您运行命令更改目录,然后递归地强制删除该目录中的所有内容( cd /some_directory ; rm -Rf *
),那么如果未进行目录更改,最终可能会导致系统毁坏。 并非我们建议您运行命令以无条件地一次删除目录中的所有文件。
![02_combining_commands_with_logical_and_operator](https://i-blog.csdnimg.cn/blog_migrate/2836fdb30c7c03a25e86794be0e5ce1d.png)
选项三:逻辑或运算符(||) (Option Three: The Logical OR Operator (||))
Sometimes you might want to execute a second command only if the first command does not succeed. To do this, we use the logical OR operator, or two vertical bars ( ||
). For example, we want to check to see if the MyFolder directory exists ( [ -d ~/MyFolder ]
) and create it if it doesn’t ( mkdir ~/MyFolder
). So, we type the following command at the prompt and press Enter.
有时你可能要执行只有在第一个命令不成功第二个命令。 为此,我们使用逻辑OR运算符或两个竖线( ||
)。 例如,我们要检查MyFolder目录是否存在( [ -d ~/MyFolder ]
),如果不存在则创建它( mkdir ~/MyFolder
)。 因此,我们在提示符下键入以下命令,然后按Enter。
[ -d ~/MyFolder ] || mkdir ~/MyFolder
Be sure there is a space after the first bracket and before the second bracket or the first command that checks if the directory exists will not work.
请确保在第一个括号之后,第二个括号之前或第一个用于检查目录是否存在的命令之前没有空格。
In our example, the MyFolder directory does not exist, so the second command creates the directory.
在我们的示例中,MyFolder目录不存在,因此第二条命令将创建目录。
![03_combining_commands_with_logical_or_operator](https://i-blog.csdnimg.cn/blog_migrate/bd87d129e92342c1d30ab5bb9295f948.png)
组合多个运算符 (Combining Multiple Operators)
You can combine multiple operators on the command line, too. For example, we want to first check if a file exists ( [ -f ~/sample.txt ]
). If it does, we print a message to the screen saying so ( echo “File exists.”
). If not, we create the file ( touch ~/sample.txt
). So, we type the following at the command prompt and press Enter.
您也可以在命令行上组合多个运算符。 例如,我们要首先检查文件是否存在( [ -f ~/sample.txt ]
)。 如果是这样,我们将在屏幕上显示一条消息,提示是这样( echo “File exists.”
)。 如果没有,我们创建文件( touch ~/sample.txt
)。 因此,我们在命令提示符下键入以下内容,然后按Enter。
[ -f ~/sample.txt ] && echo “File exists.” || touch ~/sample.txt
In our example, the file didn’t exist, so it was created.
在我们的示例中,该文件不存在,因此已创建。
![04_using_two_different_operators](https://i-blog.csdnimg.cn/blog_migrate/660053b52141b42830ffc611f4c21df3.png)
Here’s a useful summary of each of the operators used to combine commands:
这是用于组合命令的每个运算符的有用摘要:
A ; B
– Run A and then B, regardless of the success or failure of AA ; B
A ; B
–先运行A,然后运行B,无论A是成功还是失败A && B
– Run B only if A succeededA && B
–仅在A成功的情况下运行BA || B
– Run B only if A failedA || B
A || B
–仅在A失败的情况下运行B
All of these methods of combining commands can also be used in shell scripts on both Linux and Windows 10.
所有这些组合命令的方法也可以在Linux和Windows 10的 Shell脚本中使用。
You can also automatically correct spelling and typos when using “cd” on the command line in Linux to avoid drastic consequences when combining commands.
在Linux中的命令行上使用“ cd”时,您还可以自动更正拼写和错字,以避免在合并命令时产生严重后果。
翻译自: https://www.howtogeek.com/269509/how-to-run-two-or-more-terminal-commands-at-once-in-linux/
linux中一个页表有多大