{"id":2343,"date":"2017-11-18T11:26:48","date_gmt":"2017-11-18T03:26:48","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2343"},"modified":"2017-11-18T11:26:48","modified_gmt":"2017-11-18T03:26:48","slug":"shell-bash-%e7%9a%84%e9%94%99%e8%af%af%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2343","title":{"rendered":"Shell Bash \u7684\u9519\u8bef\u5904\u7406"},"content":{"rendered":"<p>\n\t\u6240\u6709\u8bed\u8a00\u90fd\u6709\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0cshell \u4e5f\u4e0d\u4f8b\u5916\u3002java \u7684\u5f02\u5e38\u5904\u7406\uff0c\u7ecf\u5e38\u88ab\u4eba\u4eec\u5410\u69fd\uff0c\u90a3\u4e48 shell \u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u53c8\u662f\u600e\u6837\u7684\u5462\uff1f\u901a\u8fc7\u672c\u6587\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u811a\u672c\u91cc\u9762\u6709\u8fd0\u884c\u5931\u8d25\u7684\u547d\u4ee4\uff08\u8fd4\u56de\u503c\u975e0\uff09\uff0cBash \u9ed8\u8ba4\u4f1a\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684\u547d\u4ee4\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n#!\/usr\/bin\/env bash\r\n\r\nfoo\r\necho bar<\/pre>\n<p>\n\t\u4e0a\u9762\u811a\u672c\u4e2d\uff0cfoo\u662f\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u547d\u4ee4\uff0c\u6267\u884c\u65f6\u4f1a\u62a5\u9519\u3002\u4f46\u662f\uff0cBash \u4f1a\u5ffd\u7565\u8fd9\u4e2a\u9519\u8bef\uff0c\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ bash script.sh\r\nscript.sh:\u884c3: foo: \u672a\u627e\u5230\u547d\u4ee4\r\nbar<\/pre>\n<p>\n\t\u53ef\u4ee5\u770b\u5230\uff0cBash \u53ea\u662f\u663e\u793a\u6709\u9519\u8bef\uff0c\u5e76\u6ca1\u6709\u7ec8\u6b62\u6267\u884c\u3002\n<\/p>\n<p>\n\t\u8fd9\u79cd\u884c\u4e3a\u5f88\u4e0d\u5229\u4e8e\u811a\u672c\u5b89\u5168\u548c\u9664\u9519\u3002\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5982\u679c\u67d0\u4e2a\u547d\u4ee4\u5931\u8d25\uff0c\u5f80\u5f80\u9700\u8981\u811a\u672c\u505c\u6b62\u6267\u884c\uff0c\u9632\u6b62\u9519\u8bef\u7d2f\u79ef\u3002\u8fd9\u65f6\uff0c\u4e00\u822c\u91c7\u7528\u4e0b\u9762\u7684\u5199\u6cd5\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\ncommand || exit 1<\/pre>\n<p>\n\t\u4e0a\u9762\u7684\u5199\u6cd5\u8868\u793a\u53ea\u8981<code>command<\/code>\u6709\u975e\u96f6\u8fd4\u56de\u503c\uff0c\u811a\u672c\u5c31\u4f1a\u505c\u6b62\u6267\u884c\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u505c\u6b62\u6267\u884c\u4e4b\u524d\u9700\u8981\u5b8c\u6210\u591a\u4e2a\u64cd\u4f5c\uff0c\u5c31\u8981\u91c7\u7528\u4e0b\u9762\u4e09\u79cd\u5199\u6cd5\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n# \u5199\u6cd5\u4e00\r\ncommand || { echo &quot;command failed&quot;; exit 1; }\r\n\r\n# \u5199\u6cd5\u4e8c\r\nif ! command; then echo &quot;command failed&quot;; exit 1; fi\r\n\r\n# \u5199\u6cd5\u4e09\r\ncommand\r\nif [ \"$?\" -ne 0 ]; then echo &quot;command failed&quot;; exit 1; fi<\/pre>\n<p>\n\t\u53e6\u5916\uff0c\u9664\u4e86\u505c\u6b62\u6267\u884c\uff0c\u8fd8\u6709\u4e00\u79cd\u60c5\u51b5\u3002\u5982\u679c\u4e24\u4e2a\u547d\u4ee4\u6709\u7ee7\u627f\u5173\u7cfb\uff0c\u53ea\u6709\u7b2c\u4e00\u4e2a\u547d\u4ee4\u6210\u529f\u4e86\uff0c\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u7b2c\u4e8c\u4e2a\u547d\u4ee4\uff0c\u90a3\u4e48\u5c31\u8981\u91c7\u7528\u4e0b\u9762\u7684\u5199\u6cd5\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\ncommand1 &amp;&amp; command2\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=2343\">Shell Bash \u7684\u9519\u8bef\u5904\u7406<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6240\u6709\u8bed\u8a00\u90fd\u6709\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0cshell \u4e5f\u4e0d\u4f8b\u5916\u3002java \u7684\u5f02\u5e38\u5904\u7406\uff0c\u7ecf\u5e38\u88ab\u4eba\u4eec\u5410\u69fd\uff0c\u90a3\u4e48 shell \u7684\u5f02\u5e38 [&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,946],"class_list":["post-2343","post","type-post","status-publish","format-standard","hentry","category-ndisdevelop","category-yyzt","tag-linux","tag-shell-","tag-946"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2343","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=2343"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2343\/revisions"}],"predecessor-version":[{"id":2344,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2343\/revisions\/2344"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}