shell命令中“&&”的目的是什麼? (What is the purpose of "&&" in a shell command?)


問題描述

shell命令中“&&”的目的是什麼? (What is the purpose of "&&" in a shell command?)

As far as I know, using & after the command is for running it in the background.

Example of & usage: tar -czf file.tar.gz dirname &

But how about &&? (example)


參考解法

方法 1:

Furthermore, you also have || which is the logical or, and also ; which is just a separator which doesn't care what happend to the command before.

$ false || echo "Oops, fail"
Oops, fail

$ true || echo "Will not be printed"
$  

$ true && echo "Things went well"
Things went well

$ false && echo "Will not be printed"
$

$ false ; echo "This will always run"
This will always run

Some details about this can be found here Lists of Commands in the Bash Manual.

方法 2:

&& lets you do something based on whether the previous command completed successfully - that's why you tend to see it chained as do_something && do_something_else_that_depended_on_something

方法 3:

  

command-line - what is the purpose of &&?

In shell, when you see 

$ command one && command two

the intent is to execute the command that follows the && only if the first command is successful. This is idiomatic of Posix shells, and not only found in Bash.

It intends to prevent the running of the second process if the first fails.

You may notice I've used the word "intent" - that's for good reason. Not all programs have the same behavior, so for this to work, you need to understand what the program considers a "failure" and how it handles it by reading the documentation and, if necessary, the source code.

Your shell considers a return value of 0 for true, other positive numbers for false

Programs return a signal on exiting. They should return 0 if they exit successfully, or greater than zero if they do not. This allows a limited amount of communication between processes.

The && is referred to as AND_IF in the posix shell grammar, which is part of an and_or list of commands, which also include the || which is an OR_IF with similar semantics. 

Grammar symbols, quoted from the documentation:

%token  AND_IF    OR_IF    DSEMI
/*      '&&'      '||'     ';;'    */

And the Grammar (also quoted from the documentation), which shows that any number of AND_IFs (&&) and/or OR_IFs (||) can be be strung together (as and_or is defined recursively):

and_or           :                         pipeline
                 | and_or AND_IF linebreak pipeline
                 | and_or OR_IF  linebreak pipeline

Both operators have equal precedence and are evaluated left to right (they are left associative). As the docs say:

  

An AND-OR list is a sequence of one or more pipelines separated by the   operators "&&" and "||" .

     

A list is a sequence of one or more AND-OR lists separated by the   operators ';' and '&' and optionally terminated by ';''&', or   .

     

The operators "&&" and "||" shall have equal precedence and shall be   evaluated with left associativity. For example, both of the following   commands write solely bar to standard output:

$ false && echo foo || echo bar
$ true || echo foo && echo bar
  1. In the first case, the false is a command that exits with the status of 1

    $ false
    $ echo $?
    1
    

    which means echo foo does not run (i.e., shortcircuiting echo foo). Then the command echo bar is executed.

  2. In the second case, true exits with a code of 0 

    $ true
    $ echo $?
    0
    

    and therefore echo foo is not executed, then echo bar is executed.

方法 4:

A quite common usage for '&&' is compiling software with autotools.  For example:

./configure --prefix=/usr && make && sudo make install

Basically if the configure succeeds, make is run to compile, and if that succeeds, make is run as root to install the program.  I use this when I am mostly sure that things will work, and it allows me to do other important things like look at stackoverflow an not 'monitor' the progress.

Sometimes I get really carried away...

tar xf package.tar.gz && ( cd package; ./configure && make && sudo make install ) && rm package -rf

I do this when for example making a linux from scratch box.

方法 5:

&& strings commands together. Successive commands only execute if preceding ones succeed.

Similarly, || will allow the successive command to execute if the preceding fails.

See Bash Shell Programming.

(by CaptainplundragirasquidRussia Must Remove PutinNigel AtkinsonDean Burge)

參考文件

  1. What is the purpose of "&&" in a shell command? (CC BY-SA 3.0/4.0)

#command-line #shell #syntax #bash






相關問題

從 FFMPEG for android 的命令行版本訪問網絡攝像頭麥克風 (Accessing webcam mic from command line version of FFMPEG for android)

為什麼我的 IDE 能找到 JAR 而我的命令行卻沒有? (Why does my IDE find the JAR but my command line doesn't?)

sbt 相當於 maven exec 插件 (sbt equivalent of maven exec plugin)

無法從 Python 的命令行導入 json (Not able to import json from commandline for Python)

在命令行上使用 VS2008 部署 SharePoint 失敗 (SharePoint deployment failure using VS2008 on the comannd line)

使用 PHP 的網頁截圖? (Web Page Screenshots with PHP?)

在命令行(終端)上使用 R 腳本的最佳方式是什麼? (What's the best way to use R scripts on the command line (terminal)?)

如何創建指向 mysql 的符號鏈接?(Mac, XAMPP) (How do I create a symbolic link to mysql? (Mac, XAMPP))

如何通過終端或其他方式將參數傳遞給 JavaScriptCore/Console? (How to pass an argument to JavaScriptCore/Console via terminal, or otherwise?)

shell命令中“&&”的目的是什麼? (What is the purpose of "&&" in a shell command?)

如何使用 dotnet test 命令行運行單個 xunit C# 測試 (How to run a single xunit C# test using dotnet test command line)

在 centOS 6.9 (Final) 中將 php-5.6 升級到 7.3 時顯示多個錯誤 (Showing multiple errors while upgrading php-5.6 to 7.3 in centOS 6.9 (Final))







留言討論