{"id":2345,"date":"2017-11-18T11:35:08","date_gmt":"2017-11-18T03:35:08","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2345"},"modified":"2017-11-18T11:35:08","modified_gmt":"2017-11-18T03:35:08","slug":"shell-bash-%e8%84%9a%e6%9c%ac-set-e-%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2345","title":{"rendered":"Shell bash \u811a\u672c set -e \u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u7a0b\u5e8f\u5728\u7f16\u5199\u9636\u6bb5\u7ecf\u5e38\u4f1a\u4ea7\u751f\u5404\u79cd\u95ee\u9898\uff0c\u4e00\u4e9b\u9ad8\u7ea7\u8bed\u8a00\u90fd\u6709\u76f8\u5bf9\u4e8e\u7684\u8c03\u8bd5\u5de5\u5177\u3002shell \u5728\u9519\u8bef\u548c\u5f02\u5e38\u5904\u7406\u65b9\u9762\u4e5f\u6709\u5bf9\u5e94\u7684\u5de5\u5177\uff0c\u5229\u7528\u5de5\u5177\u5728\u914d\u5408 set -e \uff0c\u6211\u4eec\u80fd\u66f4\u597d\u7684\u89e3\u51b3\u8bed\u6cd5\u95ee\u9898\u3002\u672c\u6587\u4ecb\u7ecd set -e \u7684\u76f8\u5173\u7528\u6cd5\u3002\n<\/p>\n<p>\n\t\u5728\u4e0a\u4e00\u7bc7\u300a<a href=\"https:\/\/www.xttblog.com\/?p=2343\" name=\"Shell Bash \u7684\u9519\u8bef\u5904\u7406\" target=\"_blank\" title=\"Shell Bash \u7684\u9519\u8bef\u5904\u7406\">Shell Bash \u7684\u9519\u8bef\u5904\u7406<\/a>\u300b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8fd9\u6837\u5904\u7406\u663e\u5f97\u975e\u5e38\u9ebb\u70e6\uff0c\u5bb9\u6613\u758f\u5ffd\u3002<code>set -e<\/code>\u4ece\u6839\u672c\u4e0a\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u5b83\u4f7f\u5f97\u811a\u672c\u53ea\u8981\u53d1\u751f\u9519\u8bef\uff0c\u5c31\u7ec8\u6b62\u6267\u884c\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n#!\/usr\/bin\/env bash\r\nset -e\r\n\r\nfoo\r\necho bar<\/pre>\n<p>\n\t\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ bash script.sh\r\nscript.sh:\u884c4: foo: \u672a\u627e\u5230\u547d\u4ee4<\/pre>\n<p>\n\t\u53ef\u4ee5\u770b\u5230\uff0c\u7b2c4\u884c\u6267\u884c\u5931\u8d25\u4ee5\u540e\uff0c\u811a\u672c\u5c31\u7ec8\u6b62\u6267\u884c\u4e86\u3002\n<\/p>\n<p>\n\t<code>set -e<\/code>\u6839\u636e\u8fd4\u56de\u503c\u6765\u5224\u65ad\uff0c\u4e00\u4e2a\u547d\u4ee4\u662f\u5426\u8fd0\u884c\u5931\u8d25\u3002\u4f46\u662f\uff0c\u67d0\u4e9b\u547d\u4ee4\u7684\u975e\u96f6\u8fd4\u56de\u503c\u53ef\u80fd\u4e0d\u8868\u793a\u5931\u8d25\uff0c\u6216\u8005\u5f00\u53d1\u8005\u5e0c\u671b\u5728\u547d\u4ee4\u5931\u8d25\u7684\u60c5\u51b5\u4e0b\uff0c\u811a\u672c\u7ee7\u7eed\u6267\u884c\u4e0b\u53bb\u3002\u8fd9\u65f6\u53ef\u4ee5\u6682\u65f6\u5173\u95ed<code>set -e<\/code>\uff0c\u8be5\u547d\u4ee4\u6267\u884c\u7ed3\u675f\u540e\uff0c\u518d\u91cd\u65b0\u6253\u5f00<code>set -e<\/code>\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\nset +e\r\ncommand1\r\ncommand2\r\nset -e<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c<code>set +e<\/code>\u8868\u793a\u5173\u95ed<code>-e<\/code>\u9009\u9879\uff0c<code>set -e<\/code>\u8868\u793a\u91cd\u65b0\u6253\u5f00<code>-e<\/code>\u9009\u9879\u3002\n<\/p>\n<p>\n\t\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528<code>command || true<\/code>\uff0c\u4f7f\u5f97\u8be5\u547d\u4ee4\u5373\u4f7f\u6267\u884c\u5931\u8d25\uff0c\u811a\u672c\u4e5f\u4e0d\u4f1a\u7ec8\u6b62\u6267\u884c\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n#!\/bin\/bash\r\nset -e\r\n\r\nfoo || true\r\necho bar<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c<code>true<\/code>\u4f7f\u5f97\u8fd9\u4e00\u884c\u8bed\u53e5\u603b\u662f\u4f1a\u6267\u884c\u6210\u529f\uff0c\u540e\u9762\u7684<code>echo bar<\/code>\u4f1a\u6267\u884c\u3002\n<\/p>\n<p>\n\t<code>-e<\/code>\u8fd8\u6709\u53e6\u4e00\u79cd\u5199\u6cd5<code>-o errexit<\/code>\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\nset -o errexit\r\n<\/pre>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2345\">Shell bash \u811a\u672c set -e \u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u7a0b\u5e8f\u5728\u7f16\u5199\u9636\u6bb5\u7ecf\u5e38\u4f1a\u4ea7\u751f\u5404\u79cd\u95ee\u9898\uff0c\u4e00\u4e9b\u9ad8\u7ea7\u8bed\u8a00\u90fd\u6709\u76f8\u5bf9\u4e8e\u7684\u8c03\u8bd5\u5de5\u5177\u3002shell \u5728\u9519\u8bef\u548c\u5f02\u5e38\u5904\u7406\u65b9\u9762\u4e5f\u6709\u5bf9\u5e94\u7684 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118,6],"tags":[706,1130],"class_list":["post-2345","post","type-post","status-publish","format-standard","hentry","category-ndisdevelop","category-yyzt","tag-linux","tag-shell-"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2345"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2345\/revisions"}],"predecessor-version":[{"id":2346,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2345\/revisions\/2346"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}