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

  • Bash Quick Start Guide
  • Tom Ryder
  • 191字
  • 2021-07-23 16:58:58

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.

主站蜘蛛池模板: 通城县| 浦城县| 巴青县| 渝北区| 尉氏县| 抚宁县| 金阳县| 河津市| 武胜县| 昌江| 新宁县| 乐东| 周宁县| 荆门市| 当雄县| 娄底市| 托克托县| 定兴县| 铜陵市| 会东县| 博罗县| 靖西县| 石阡县| 雷波县| 邵东县| 都昌县| 望都县| 扶风县| 墨玉县| 新龙县| 黄梅县| 博野县| 偏关县| 轮台县| 黄平县| 临沭县| 临澧县| 翁牛特旗| 福贡县| 梅州市| 东乌珠穆沁旗|