linux中一个页表有多大_如何在Linux中一次运行两个或更多终端命令

linux中一个页表有多大

linux中一个页表有多大

00_lead_image_combining_commands

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

选项二:逻辑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

选项三:逻辑或运算符(||) (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

组合多个运算符 (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

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 A

    A ; B A ; B –先运行A,然后运行B,无论A是成功还是失败

  •  A && B  – Run B only if A succeeded

    A && B –仅在A成功的情况下运行B

  •  A || B  – Run B only if A failed

    A || 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中一个页表有多大

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值