{"id":7019,"date":"2023-01-08T16:36:13","date_gmt":"2023-01-08T08:36:13","guid":{"rendered":"https:\/\/www.chenqing.work\/?p=7019"},"modified":"2023-01-08T16:51:19","modified_gmt":"2023-01-08T08:51:19","slug":"%e8%af%91-shell-script-best-practices","status":"publish","type":"post","link":"https:\/\/www.chenqing.work\/?p=7019","title":{"rendered":"\u8bd1 | Shell Script Best Practices"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1440\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2023\/01\/gabriel-heinzer-4Mw7nkQDByk-unsplash.jpg\" alt=\"\" class=\"wp-image-7021\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u5468\u4e0d\u592a\u60f3\u5199\u957f\u6587\u7ae0\uff0c\u5148\u7ffb\u8bd1\u4e00\u7bc7\u5145\u5145\u6570<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u539f\u6587\uff1a<a href=\"https:\/\/sharats.me\/posts\/shell-script-best-practices\/\" data-type=\"URL\" data-id=\"https:\/\/sharats.me\/posts\/shell-script-best-practices\/\">Shell Script Best Practices<\/a><\/p>\n\n\n\n<p>\u672c\u6587\u662f\u4e2a\u4eba\u8fd9\u4e9b\u5e74\u5199 shell \u811a\u672c\u79ef\u7d2f\u7684\u7ecf\u9a8c\u6cd5\u5219\uff0c\u4ee5\u98e8\u8bfb\u8005\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89c4\u5219<\/h2>\n\n\n\n<p>1\u3001<strong>\u4f7f\u7528 bash<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528 zsh\u3001fish \u5c06\u4f7f\u5f97\u5176\u4ed6\u4eba\u96be\u4e8e\u7406\u89e3\u548c\u5408\u4f5c\u3002\u5728\u6240\u6709\u7684 shell \u4e2d\uff0cbash \u5728\u53ef\u79fb\u690d\u6027\u548c DX(?) \u4e4b\u95f4\u53d6\u5f97\u4e86\u4e00\u4e2a\u6bd4\u8f83\u597d\u7684\u5e73\u8861\u3002<\/p>\n\n\n\n<p>2\u3001<strong>\u7b2c\u4e00\u884c\u5199 <em>#!\/usr\/bin\/env bash<\/em><\/strong>\uff0c\u5373\u4f7f\u6ca1\u6709\u653e\u5f00\u811a\u672c\u6587\u4ef6\u7684\u53ef\u6267\u884c\u6743\u9650<\/p>\n\n\n\n<p>\u8fdb\u5165 bash \u6267\u884c\u73af\u5883\u3002<\/p>\n\n\n\n<p>3\u3001<strong>\u4f7f\u7528 .sh \/ .bash \u4f5c\u4e3a\u811a\u672c\u6587\u4ef6\u6269\u5c55\u540d<\/strong><\/p>\n\n\n\n<p>\u4e0d\u7ed9\u811a\u672c\u8bbe\u7f6e\u6269\u5c55\u540d\u53ef\u80fd\u4f1a\u663e\u5f97\u6bd4\u8f83\u82b1\u54e8\uff0c\u4f46\u9664\u975e\u4f60\u5728\u76ee\u524d\u7684\u573a\u666f\u4e2d\u660e\u786e\u9700\u8981\u8fd9\u6837\uff0c\u4f60\u53ef\u80fd\u4ec5\u4ec5\u662f\u60f3\u505a\u70b9\u806a\u660e\u4e8b\u3002\u806a\u660e\u4e8b\u901a\u5e38\u96be\u4ee5\u7406\u89e3\u3002<\/p>\n\n\n\n<p>4\u3001<strong>\u811a\u672c\u5f00\u5934\u5199\u4e0a set -o errexit<\/strong><\/p>\n\n\n\n<p>\u4f5c\u7528\u662f\u5f53\u67d0\u4e00\u884c\u547d\u4ee4\u5931\u8d25\u65f6\uff0c\u811a\u672c\u4f1a\u76f4\u63a5\u9000\u51fa\uff0c\u800c\u4e0d\u4f1a\u7ee7\u7eed\u6267\u884c\u5269\u4f59\u7684\u547d\u4ee4\u3002<\/p>\n\n\n\n<p>5\u3001<strong>\u9996\u9009\u4f7f\u7528 <em>set -o nounset<\/em><\/strong><\/p>\n\n\n\n<p>\u4f60\u53ef\u80fd\u6709\u5f88\u597d\u7684\u7406\u7531\u4e0d\u8fd9\u6837\u5199\uff0c\u4f46\u6211\u7684\u89c2\u70b9\u662f\u6700\u597d\u5199\u4e0a\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u811a\u672c\u8bbf\u95ee\u4e86\u672a\u8bbe\u7f6e\u7684\u53d8\u91cf\u65f6\uff0c\u6267\u884c\u5931\u8d25\u3002\u4fdd\u969c\u4e0d\u4f1a\u9677\u5165\u7531\u4e8e\u53d8\u91cf\u540d\u62fc\u5199\u9519\u8bef\u5bfc\u81f4\u7684\u9884\u671f\u5916\u7684\u53ef\u6015\u7ed3\u679c\u4e2d\uff1b<\/li>\n\n\n\n<li>\u5f53\u9700\u8981\u8bbf\u95ee\u4e00\u4e2a\u53ef\u80fd\u672a\u8bbe\u7f6e\u7684\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528 <em>&#8220;${VARNAME-}&#8221;<\/em> \u4ee3\u66ff <em>&#8220;$VARNAME&#8221;<\/em>\u3002<\/li>\n<\/ul>\n\n\n\n<p>6\u3001<strong>\u4f7f\u7528 <em>set -o pipefail<\/em><\/strong><\/p>\n\n\n\n<p>\u4f5c\u7528\u662f\u4fdd\u969c\u7ba1\u9053\u547d\u4ee4\u4e2d\u4efb\u4f55\u4e00\u4e2a\u547d\u4ee4\u5931\u8d25\uff0c\u7ba1\u9053\u7684\u7ed3\u679c\u5c31\u662f\u5931\u8d25\u3002<\/p>\n\n\n\n<p>7\u3001<strong>\u4f7f\u7528 <em>set -o xtrace<\/em><\/strong>\uff0c\u4f34\u968f\u4e00\u4e2a\u5bf9 <em>$TRACE<\/em> \u73af\u5883\u53d8\u91cf\u7684\u68c0\u67e5<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8e copy-paste \u6a21\u5f0f\uff1a<em>if [[ &#8220;${TRACE-0}&#8221; == &#8220;1&#8221; ]]; then set -o xtrace; fi.<\/em> \uff1b<\/li>\n\n\n\n<li>\u5bf9 debug \u811a\u672c\u5f88\u6709\u5e2e\u52a9\uff1b<\/li>\n\n\n\n<li>\u73b0\u5728\u652f\u6301\u5f00\u542f debug \u6a21\u5f0f\uff0c\u53ea\u8981\u5728\u8fd0\u884c\u65f6\u4f7f\u7528 <em>TRACE=1 .\/script.sh<\/em> \u4ee3\u66ff <em>.\/script.sh<\/em> \u3002<\/li>\n<\/ul>\n\n\n\n<p>8\u3001<strong>\u4f7f\u7528 <em>[[ ]]<\/em> \u4f5c\u4e3a if\/while \u4e2d\u7684\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u4ee3\u66ff <em>[ ]<\/em> \u6216 <em>test<\/em><\/strong><\/p>\n\n\n\n<p><em>[[ ]]<\/em> \u662f\u4e00\u4e2a\u6bd4 <em>[]<\/em> \u6216 <em>test <\/em>\u66f4\u5f3a\u5927\u7684\u5173\u952e\u5b57\u3002<\/p>\n\n\n\n<p>9\u3001<strong>\u8bbf\u95ee\u53d8\u91cf\u65f6\u7528\u53cc\u5f15\u53f7\u62ec\u8d77\u6765<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u53d8\u91cf\u4f4d\u4e8e [[ ]] \u4e2d\u5224\u65ad\u6761\u4ef6\u7684\u5de6\u4fa7\u65f6\u53ef\u4ee5\u4e0d\u91c7\u7528\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\uff0c\u4f46\u5373\u4f7f\u8fd9\u79cd\u573a\u666f\uff0c\u6211\u4e5f\u5efa\u8bae\u62ec\u8d77\u6765\uff1b<\/li>\n\n\n\n<li>\u5f53\u9700\u8981\u4e0d\u62ec\u8d77\u6765\u7684\u4f7f\u7528\u65b9\u5f0f\u65f6\uff0c\u91c7\u7528 bash \u6570\u7ec4\u4f1a\u66f4\u597d\u3002<\/li>\n<\/ul>\n\n\n\n<p>10\u3001<strong>\u5728\u65b9\u6cd5\u4e2d\u4f7f\u7528 <em>local <\/em> \u58f0\u660e\u53d8\u91cf<\/strong><\/p>\n\n\n\n<p>11\u3001<strong>\u652f\u6301\u591a\u79cd\u7528\u6237\u6c42\u52a9\u65b9\u5f0f\u6216\u63d0\u4f9b\u53cb\u597d\u54cd\u5e94<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4fdd\u969c\u811a\u672c\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f <em>-h<\/em>\u3001<em>&#8211;help<\/em>\u3001<em>help<\/em>\u3001<em>h<\/em>\u3001\u751a\u81f3 <em>-help<\/em> \u65f6\uff0c\u80fd\u6253\u5370\u5e2e\u52a9\u5e76\u9000\u51fa\uff1b<\/li>\n\n\n\n<li>\u8bf7\u4e3a\u672a\u6765\u7684\u81ea\u5df1\u7740\u60f3\u4e00\u4e0b\u3002<\/li>\n<\/ul>\n\n\n\n<p>12\u3001<strong>\u9519\u8bef\u6d88\u606f\u91cd\u5b9a\u5411\u5230 stderr<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528 <em>echo &#8216;\u53d1\u751f\u4e86\u672a\u9884\u671f\u7684\u4e8b\u60c5&#8217;  >&amp;2<\/em>  \u3002<\/p>\n\n\n\n<p>13\u3001<strong>\u53ea\u8981\u53ef\u80fd\uff0c\u5c31\u4f7f\u7528\u957f\u7684\u9009\u9879\u547d\u540d\u65b9\u5f0f\uff08\u5982\u4f7f\u7528 &#8211;silent \u4ee3\u66ff -s\uff09<\/strong><\/p>\n\n\n\n<p>\u8fd9\u79cd\u547d\u540d\u65b9\u5f0f\u6709\u52a9\u4e8e\u663e\u5f0f\u8bf4\u660e\u547d\u4ee4\u7528\u9014\u3002<\/p>\n\n\n\n<p>\u4e0d\u8fc7\u8bf7\u6ce8\u610f\uff0c\u67d0\u4e9b\u7cfb\u7edf\u4e0a\u63d0\u4f9b\u7684\u547d\u4ee4\u2014\u2014\u6bd4\u5982 macOS\uff0c\u5e76\u4e0d\u603b\u662f\u6709\u957f\u547d\u540d\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>14\u3001<strong>\u5408\u9002\u7684\u8bdd\uff0c\u5728\u811a\u672c\u6b63\u5f0f\u5f00\u59cb\u5de5\u4f5c\u4e4b\u524d\u5c06\u5de5\u4f5c\u8def\u5f84\u5207\u6362\u5230\u811a\u672c\u6240\u5728\u7684\u8def\u5f84\u4e0b<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5176\u5b9e\u603b\u662f\u5408\u9002\u7684\uff1b<\/li>\n\n\n\n<li>\u4f7f\u7528 <em>cd &#8220;$(dirname &#8220;$0&#8243;)&#8221;<\/em>\uff0c\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u90fd\u6709\u6548 \u3002<\/li>\n<\/ul>\n\n\n\n<p>15\u3001 <strong>\u4f7f\u7528 <em>shellcheck<\/em>. \u6ce8\u610f\u5b83\u7684\u544a\u8b66\u63d0\u793a\u3002<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u677f<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/usr\/bin\/env bash\n\nset -o errexit\nset -o nounset\nset -o pipefail\nif &#x5B;&#x5B; &quot;${TRACE-0}&quot; == &quot;1&quot; ]]; then\n    set -o xtrace\nfi\n\nif &#x5B;&#x5B; &quot;${1-}&quot; =~ ^-*h(elp)?$ ]]; then\n    echo 'Usage: .\/script.sh arg-one arg-two\n\nThis is an awesome bash script to make your life better.\n\n'\n    exit\nfi\n\ncd &quot;$(dirname &quot;$0&quot;)&quot;\n\nmain() {\n    echo do awesome stuff\n}\n\nmain &quot;$@&quot;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u7ed3\u8bba<\/h2>\n\n\n\n<p>\u6211\u5c3d\u529b\u9075\u5b88\u8fd9\u4e9b\u89c4\u5219\uff0c\u81f3\u5c11\u8fd9\u6837\u8ba9\u6211\u7684\u751f\u6d3b\u66f4\u7f8e\u597d\u4e86\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u6211\u5e76\u4e0d\u603b\u662f\u80fd\u5b8c\u5168\u9075\u5b88\u81ea\u5df1\u603b\u7ed3\u7684\u8fd9\u4e9b\u89c4\u5219\u3002So\uff0c\u4e5f\u8bb8\u5199\u4e0b\u6765\u4e5f\u5bf9\u6211\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u5468\u4e0d\u592a\u60f3\u5199\u957f\u6587\u7ae0\uff0c\u5148\u7ffb\u8bd1\u4e00\u7bc7\u5145\u5145\u6570 \u539f\u6587\uff1aShell Script Best Practices  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[136,135],"class_list":["post-7019","post","type-post","status-publish","format-standard","hentry","category-code","tag-bash","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/7019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7019"}],"version-history":[{"count":41,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/7019\/revisions"}],"predecessor-version":[{"id":7061,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/7019\/revisions\/7061"}],"wp:attachment":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}