{"id":2347,"date":"2017-11-18T11:39:43","date_gmt":"2017-11-18T03:39:43","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2347"},"modified":"2017-11-18T11:39:43","modified_gmt":"2017-11-18T03:39:43","slug":"shell-bash-%e8%84%9a%e6%9c%ac-set-o-pipefail-%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2347","title":{"rendered":"Shell bash \u811a\u672c set -o pipefail \u6559\u7a0b"},"content":{"rendered":"<p>\n\t<code>set -e<\/code>\u6709\u4e00\u4e2a\u4f8b\u5916\u60c5\u51b5\uff0c\u5c31\u662f\u4e0d\u9002\u7528\u4e8e\u7ba1\u9053\u547d\u4ee4\u3002\n<\/p>\n<p>\n\t\u6240\u8c13\u7ba1\u9053\u547d\u4ee4\uff0c\u5c31\u662f\u591a\u4e2a\u5b50\u547d\u4ee4\u901a\u8fc7\u7ba1\u9053\u8fd0\u7b97\u7b26\uff08<code>|<\/code>\uff09\u7ec4\u5408\u6210\u4e3a\u4e00\u4e2a\u5927\u7684\u547d\u4ee4\u3002Bash \u4f1a\u628a\u6700\u540e\u4e00\u4e2a\u5b50\u547d\u4ee4\u7684\u8fd4\u56de\u503c\uff0c\u4f5c\u4e3a\u6574\u4e2a\u547d\u4ee4\u7684\u8fd4\u56de\u503c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u8981\u6700\u540e\u4e00\u4e2a\u5b50\u547d\u4ee4\u4e0d\u5931\u8d25\uff0c\u7ba1\u9053\u547d\u4ee4\u603b\u662f\u4f1a\u6267\u884c\u6210\u529f\uff0c\u56e0\u6b64\u5b83\u540e\u9762\u547d\u4ee4\u4f9d\u7136\u4f1a\u6267\u884c\uff0c<code>set -e<\/code>\u5c31\u5931\u6548\u4e86\u3002\n<\/p>\n<p>\n\t\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n#!\/usr\/bin\/env bash\r\nset -e\r\n\r\nfoo | echo a\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\na\r\nscript.sh:\u884c4: foo: \u672a\u627e\u5230\u547d\u4ee4\r\nbar<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c<code>foo<\/code>\u662f\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u547d\u4ee4\uff0c\u4f46\u662f<code>foo | echo a<\/code>\u8fd9\u4e2a\u7ba1\u9053\u547d\u4ee4\u4f1a\u6267\u884c\u6210\u529f\uff0c\u5bfc\u81f4\u540e\u9762\u7684<code>echo bar<\/code>\u4f1a\u7ee7\u7eed\u6267\u884c\u3002\n<\/p>\n<p>\n\t<code>set -o pipefail<\/code>\u7528\u6765\u89e3\u51b3\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ea\u8981\u4e00\u4e2a\u5b50\u547d\u4ee4\u5931\u8d25\uff0c\u6574\u4e2a\u7ba1\u9053\u547d\u4ee4\u5c31\u5931\u8d25\uff0c\u811a\u672c\u5c31\u4f1a\u7ec8\u6b62\u6267\u884c\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n#!\/usr\/bin\/env bash\r\nset -eo pipefail\r\n\r\nfoo | echo a\r\necho bar<\/pre>\n<p>\n\t\u8fd0\u884c\u540e\uff0c\u7ed3\u679c\u5982\u4e0b\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ bash script.sh\r\na\r\nscript.sh:\u884c4: foo: \u672a\u627e\u5230\u547d\u4ee4<\/pre>\n<p>\n\t\u53ef\u4ee5\u770b\u5230\uff0cecho bar\u6ca1\u6709\u6267\u884c\u3002\n<\/p>\n<p>\n\tset\u547d\u4ee4\u7684-u\/-e\/-x\/&nbsp;-o pipefail\u8fd9\u56db\u4e2a\u53c2\u6570\uff0c\u4e00\u822c\u90fd\u653e\u5728\u4e00\u8d77\u4f7f\u7528\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n# \u5199\u6cd5\u4e00\r\nset -euxo pipefail\r\n\r\n# \u5199\u6cd5\u4e8c\r\nset -eux\r\nset -o pipefail<\/pre>\n<p>\n\t\u8fd9\u4e24\u79cd\u5199\u6cd5\u5efa\u8bae\u653e\u5728\u6240\u6709 Bash \u811a\u672c\u7684\u5934\u90e8\u3002\n<\/p>\n<p>\n\t\u53e6\u4e00\u79cd\u529e\u6cd5\u662f\u5728\u6267\u884c Bash \u811a\u672c\u7684\u65f6\u5019\uff0c\u4ece\u547d\u4ee4\u884c\u4f20\u5165\u8fd9\u4e9b\u53c2\u6570\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ bash -euxo pipefail script.sh<\/pre>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/The-Set-Builtin.html\" target=\"_blank\">The Set Builtin<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/vaneyckt.io\/posts\/safer_bash_scripts_with_set_euxo_pipefail\/\" target=\"_blank\">Safer bash scripts with &#039;set -euxo pipefail&#039;<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.davidpashley.com\/articles\/writing-robust-shell-scripts\/\" target=\"_blank\">Writing Robust Bash Shell Scripts<\/a>\n\t<\/li>\n<\/ul>\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=2347\">Shell bash \u811a\u672c set -o pipefail \u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>set -e\u6709\u4e00\u4e2a\u4f8b\u5916\u60c5\u51b5\uff0c\u5c31\u662f\u4e0d\u9002\u7528\u4e8e\u7ba1\u9053\u547d\u4ee4\u3002 \u6240\u8c13\u7ba1\u9053\u547d\u4ee4\uff0c\u5c31\u662f\u591a\u4e2a\u5b50\u547d\u4ee4\u901a\u8fc7\u7ba1\u9053\u8fd0\u7b97\u7b26\uff08|\uff09\u7ec4\u5408\u6210\u4e3a\u4e00 [&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":[1440,706,1130],"class_list":["post-2347","post","type-post","status-publish","format-standard","hentry","category-ndisdevelop","category-yyzt","tag-bash","tag-linux","tag-shell-"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2347","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=2347"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2347\/revisions"}],"predecessor-version":[{"id":2348,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2347\/revisions\/2348"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}