官术网_书友最值得收藏!

Stopping a command list on error

Most of the time when programming in Bash, you will not actually want to test $? directly, but instead test it implicitly as success or failure, with language features in Bash itself.

If you wanted to issue a set of commands on one command line, but only to continue if every command worked, you would use the double-ampersand (&&) control operator, instead of the semicolon (;):

$ cd && rmdir ~/nonexistent && ls

When we run this command line, we see that the final ls never runs, because the rmdir command before it failed:

rmdir: failed to remove '/home/user/nonexistent': No such file or directory

Similarly, if we changed the cd command at the start of the command line to change into a directory that didn't exist, the command line would stop even earlier:

bash$ cd ~/nonexistent && rmdir ~/nonexistent && ls
bash: cd: /home/bashuser/nonexistent: No such file or directory

In Chapter 6, Loops and Conditionals, we'll explore more fully Bash's options for control flow, including using the || command separator, and using the if command to execute blocks of code conditional on a test outcome.

主站蜘蛛池模板: 赣州市| 扎鲁特旗| 聊城市| 广丰县| 马公市| 松阳县| 德令哈市| 合川市| 镇康县| 彭州市| 达日县| 阿合奇县| 宝坻区| 平湖市| 繁昌县| 枝江市| 蓝山县| 东源县| 泉州市| 漳州市| 闸北区| 青浦区| 古交市| 嘉义县| 辽阳县| 安国市| 镇坪县| 惠水县| 赤壁市| 松阳县| 宜君县| 修武县| 正镶白旗| 峨边| 金沙县| 桐柏县| 二连浩特市| 沁源县| 安仁县| 蚌埠市| 青河县|