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

  • 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.

主站蜘蛛池模板: 鸡泽县| 廊坊市| 吴江市| 依安县| 河北省| 穆棱市| 平和县| 绵阳市| 铜鼓县| 武夷山市| 南乐县| 崇仁县| 仲巴县| 汨罗市| 深州市| 赤壁市| 綦江县| 祁门县| 绿春县| 宁河县| 兰溪市| 莱阳市| 永康市| 桂阳县| 陆丰市| 商城县| 阳江市| 宝鸡市| 噶尔县| 田东县| 巴东县| 云龙县| 松滋市| 泰和县| 彩票| 九龙坡区| 伊春市| 隆子县| 博湖县| 凤庆县| 米易县|