{"id":353,"date":"2025-11-03T15:56:04","date_gmt":"2025-11-03T07:56:04","guid":{"rendered":"https:\/\/qkd.koudaipc.com\/?p=353"},"modified":"2025-11-03T15:56:04","modified_gmt":"2025-11-03T07:56:04","slug":"posix-shell-%e7%bc%a9%e8%bf%9b%e6%a0%bc%e5%bc%8f%e8%a7%84%e8%8c%83%e6%8e%a8%e8%8d%90","status":"publish","type":"post","link":"https:\/\/qkd.koudaipc.com\/en\/2025\/11\/03\/posix-shell-%e7%bc%a9%e8%bf%9b%e6%a0%bc%e5%bc%8f%e8%a7%84%e8%8c%83%e6%8e%a8%e8%8d%90\/","title":{"rendered":"POSIX Shell \u7f29\u8fdb\u683c\u5f0f\u89c4\u8303\u63a8\u8350"},"content":{"rendered":"<p>POSIX shell \u811a\u672c\u7684\u7f29\u8fdb\u683c\u5f0f\u867d\u7136\u6ca1\u6709\u5b98\u65b9\u6807\u51c6\uff0c\u4f46\u6709\u4ee5\u4e0b\u5e7f\u4e3a\u63a5\u53d7\u7684\u5b9e\u8df5\u89c4\u8303\uff1a<\/p>\n<h2>\u57fa\u672c\u7f29\u8fdb\u89c4\u5219<\/h2>\n<h3>1. \u7f29\u8fdb\u5927\u5c0f<\/h3>\n<ul>\n<li><strong>\u63a8\u8350\u4f7f\u7528 2 \u6216 4 \u4e2a\u7a7a\u683c<\/strong><\/li>\n<li>\u907f\u514d\u4f7f\u7528\u5236\u8868\u7b26\uff08Tab\uff09\u4ee5\u4fdd\u8bc1\u8de8\u73af\u5883\u4e00\u81f4\u6027<\/li>\n<\/ul>\n<pre><code class=\"language-sh\"># \u597d\u7684\u793a\u4f8b - 2\u7a7a\u683c\u7f29\u8fdb\nif [ &quot;$var&quot; = &quot;value&quot; ]; then\n  echo &quot;Condition met&quot;\n  perform_task\nfi\n\n# \u597d\u7684\u793a\u4f8b - 4\u7a7a\u683c\u7f29\u8fdb\nif [ &quot;$var&quot; = &quot;value&quot; ]; then\n    echo &quot;Condition met&quot;\n    perform_task\nfi<\/code><\/pre>\n<h3>2. \u4ee3\u7801\u5757\u7f29\u8fdb<\/h3>\n<pre><code class=\"language-sh\"># if\/then\/else\nif condition; then\n    command1\n    command2\nelif other_condition; then\n    command3\nelse\n    command4\nfi\n\n# for \u5faa\u73af\nfor item in $list; do\n    process &quot;$item&quot;\n    another_command\ndone\n\n# while \u5faa\u73af\nwhile condition; do\n    process_data\n    update_state\ndone\n\n# case \u8bed\u53e5\ncase $variable in\n    pattern1)\n        command1\n        command2\n        ;;\n    pattern2)\n        command3\n        ;;\n    *)\n        default_command\n        ;;\nesac<\/code><\/pre>\n<h3>3. \u51fd\u6570\u5b9a\u4e49<\/h3>\n<pre><code class=\"language-sh\">my_function() {\n    local var1=&quot;$1&quot;\n    local var2=&quot;$2&quot;\n\n    if [ -z &quot;$var1&quot; ]; then\n        echo &quot;Error: var1 is empty&quot;\n        return 1\n    fi\n\n    process_data &quot;$var1&quot; &quot;$var2&quot;\n}<\/code><\/pre>\n<h2>\u9ad8\u7ea7\u683c\u5f0f\u89c4\u8303<\/h2>\n<h3>4. \u7ba1\u9053\u548c\u591a\u884c\u547d\u4ee4<\/h3>\n<pre><code class=\"language-sh\"># \u957f\u7ba1\u9053\u547d\u4ee4\nfind \/path -name &quot;*.txt&quot; \\\n    | grep -v &quot;temp&quot; \\\n    | sort -r \\\n    | head -10\n\n# \u590d\u6742\u547d\u4ee4\ncurl -s -X GET \\\n    -H &quot;Content-Type: application\/json&quot; \\\n    -H &quot;Authorization: Bearer $TOKEN&quot; \\\n    &quot;https:\/\/api.example.com\/data&quot;<\/code><\/pre>\n<h3>5.  heredoc \u7f29\u8fdb<\/h3>\n<pre><code class=\"language-sh\"># \u7f29\u8fdb\u7684 heredoc (&lt;&lt;- \u5141\u8bb8\u524d\u5bfc\u5236\u8868\u7b26)\nif [ &quot;$use_heredoc&quot; = &quot;yes&quot; ]; then\n    cat &lt;&lt;- EOF\n    This is a heredoc\n    With indented content\n    EOF\nfi<\/code><\/pre>\n<h2>\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h2>\n<h3>6. \u4e00\u81f4\u6027\u89c4\u5219<\/h3>\n<ul>\n<li><strong>\u9009\u62e9\u4e00\u79cd\u98ce\u683c\u5e76\u5728\u6574\u4e2a\u9879\u76ee\u4e2d\u4fdd\u6301\u7edf\u4e00<\/strong><\/li>\n<li>\u56e2\u961f\u9879\u76ee\u5e94\u5236\u5b9a\u5e76\u9075\u5b88\u5171\u540c\u7684\u7f16\u7801\u89c4\u8303<\/li>\n<li>\u4f7f\u7528 shellcheck \u8fdb\u884c\u8bed\u6cd5\u68c0\u67e5<\/li>\n<\/ul>\n<h3>7. \u6587\u4ef6\u5934\u683c\u5f0f<\/h3>\n<pre><code class=\"language-sh\">#!\/bin\/sh\n#\n# \u811a\u672c\u540d\u79f0: deployment.sh\n# \u63cf\u8ff0: \u5e94\u7528\u90e8\u7f72\u811a\u672c\n# \u4f5c\u8005: \u59d3\u540d\n# \u521b\u5efa\u65e5\u671f: 2024-01-01\n#\n# \u7528\u6cd5: .\/deployment.sh [environment]\n# \u53c2\u6570: environment - \u90e8\u7f72\u73af\u5883 (prod\/staging)\n#\n\nset -euo pipefail  # \u4e25\u683c\u6a21\u5f0f<\/code><\/pre>\n<h3>8. \u6ce8\u91ca\u89c4\u8303<\/h3>\n<pre><code class=\"language-sh\"># \u5355\u884c\u6ce8\u91ca\n## \u91cd\u8981\u8bf4\u660e\u4f7f\u7528\u53cc\u4e95\u53f7\n# \n# \u591a\u884c\u6ce8\u91ca\u5757\n# \u7528\u4e8e\u63cf\u8ff0\u590d\u6742\u903b\u8f91\n#\n\n: &#039;\n\u8fd9\u4e5f\u662f\u591a\u884c\u6ce8\u91ca\u7684\n\u4e00\u79cd\u66ff\u4ee3\u65b9\u5f0f\n&#039;<\/code><\/pre>\n<h2>\u5de5\u5177\u63a8\u8350<\/h2>\n<h3>\u683c\u5f0f\u5316\u5de5\u5177<\/h3>\n<ul>\n<li><strong>shfmt<\/strong>: \u81ea\u52a8\u683c\u5f0f\u5316 shell \u811a\u672c<\/li>\n<li><strong>beautysh<\/strong>: \u53e6\u4e00\u4e2a shell \u683c\u5f0f\u5316\u5de5\u5177<\/li>\n<li><strong>shellcheck<\/strong>: \u9759\u6001\u5206\u6790\u5de5\u5177\uff08\u4e0d\u683c\u5f0f\u5316\u4f46\u68c0\u67e5\u8bed\u6cd5\uff09<\/li>\n<\/ul>\n<h3>.editorconfig \u793a\u4f8b<\/h3>\n<pre><code class=\"language-ini\"># .editorconfig\nroot = true\n\n[*]\ncharset = utf-8\nend_of_line = lf\ninsert_final_newline = true\ntrim_trailing_whitespace = true\n\n[*.sh]\nindent_style = space\nindent_size = 2<\/code><\/pre>\n<p>\u9009\u62e9\u6700\u9002\u5408\u4f60\u9879\u76ee\u7684\u7f29\u8fdb\u98ce\u683c\u5e76\u4fdd\u6301\u4e00\u81f4\u6027\u662f\u6700\u91cd\u8981\u7684\u539f\u5219\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>POSIX shell \u811a\u672c\u7684\u7f29\u8fdb\u683c\u5f0f\u867d\u7136\u6ca1\u6709\u5b98\u65b9\u6807\u51c6\uff0c\u4f46\u6709\u4ee5\u4e0b\u5e7f\u4e3a\u63a5\u53d7\u7684\u5b9e\u8df5\u89c4\u8303\uff1a \u57fa\u672c\u7f29\u8fdb\u89c4\u5219 1. \u7f29\u8fdb [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","footnotes":""},"categories":[23],"tags":[],"class_list":["post-353","post","type-post","status-publish","format-standard","hentry","category-kpcos","pmpro-has-access"],"_links":{"self":[{"href":"https:\/\/qkd.koudaipc.com\/en\/wp-json\/wp\/v2\/posts\/353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qkd.koudaipc.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qkd.koudaipc.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qkd.koudaipc.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/qkd.koudaipc.com\/en\/wp-json\/wp\/v2\/comments?post=353"}],"version-history":[{"count":1,"href":"https:\/\/qkd.koudaipc.com\/en\/wp-json\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":354,"href":"https:\/\/qkd.koudaipc.com\/en\/wp-json\/wp\/v2\/posts\/353\/revisions\/354"}],"wp:attachment":[{"href":"https:\/\/qkd.koudaipc.com\/en\/wp-json\/wp\/v2\/media?parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qkd.koudaipc.com\/en\/wp-json\/wp\/v2\/categories?post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qkd.koudaipc.com\/en\/wp-json\/wp\/v2\/tags?post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}