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

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

主站蜘蛛池模板: 堆龙德庆县| 桐柏县| 澄迈县| 龙岩市| 深水埗区| 西平县| 瑞丽市| 怀仁县| 陇西县| 邵阳县| 清徐县| 铜川市| 常熟市| 恭城| 乌拉特后旗| 桃园市| 二手房| 赤城县| 延寿县| 财经| 台东市| 拉萨市| 长宁区| 象州县| 武川县| 三穗县| 北票市| 柘城县| 北票市| 蒙山县| 宜昌市| 库车县| 武定县| 十堰市| 柘荣县| 蛟河市| 漳浦县| 天台县| 垦利县| 抚松县| 文山县|