{"id":63861,"date":"2025-04-29T12:20:20","date_gmt":"2025-04-29T04:20:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/63861\/"},"modified":"2025-04-29T12:20:20","modified_gmt":"2025-04-29T04:20:20","slug":"linux-shell%e8%84%9a%e6%9c%ac%e6%80%8e%e4%b9%88%e5%86%99-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/63861\/","title":{"rendered":"linux\u00a0shell\u811a\u672c\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  linux shell \u811a\u672c\u7f16\u5199\u6307\u5357\u4e2d\u521b\u5efa\u811a\u672c\u7684\u5173\u952e\u6b65\u9aa4\u6709\uff1a1. \u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u4ee5 &#8220;.sh&#8221; \u6269\u5c55\u540d\u521b\u5efa\u65b0\u6587\u4ef6\uff1b2. \u4f7f\u7528 &#8220;chmod +x&#8221; \u8bbe\u7f6e\u53ef\u6267\u884c\u6743\u9650\u30023. shell \u811a\u672c\u7ed3\u6784\u5305\u542b shebang \u884c\u3001\u6ce8\u91ca\u3001\u53d8\u91cf\u3001\u547d\u4ee4\u3001\u6761\u4ef6\u548c\u5faa\u73af\u8bed\u53e5\u3002\u57fa\u672c\u8bed\u6cd5\u6d89\u53ca\u6253\u5370\u8f93\u51fa\u3001\u53d8\u91cf\u4f7f\u7528\u3001\u7ba1\u9053\u3001\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af\u8bed\u53e5\u3002\u5b9e\u7528\u6280\u5de7\u5305\u62ec\u4f7f\u7528\u901a\u914d\u7b26\u3001\u91cd\u5b9a\u5411\u8f93\u5165\/\u8f93\u51fa\u3001\u8c03\u8bd5\u548c\u9519\u8bef\u5904\u7406\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/18\/2024061820493049198.jpg\" class=\"aligncenter\" title=\"linux\u00a0shell\u811a\u672c\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"linux\u00a0shell\u811a\u672c\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>Linux Shell \u811a\u672c\u7f16\u5199\u6307\u5357<\/strong><\/p>\n<p><strong>1. \u521b\u5efa\u811a\u672c<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982 nano \u6216 vim\uff09\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\uff0c\u5e76\u4ee5 &#8220;.sh&#8221; \u4f5c\u4e3a\u6269\u5c55\u540d\u3002<\/li>\n<li>\u4f8b\u5982\uff1anano my_script.sh<\/li>\n<\/ul>\n<p><strong>2. \u8bbe\u7f6e\u53ef\u6267\u884c\u6743\u9650<\/strong><\/p>\n<ul>\n<li>\u5b8c\u6210\u811a\u672c\u7f16\u5199\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u53ef\u6267\u884c\u6743\u9650\uff1a<\/li>\n<li>chmod +x my_script.sh<\/li>\n<\/ul>\n<p><strong>3. Shell \u811a\u672c\u7ed3\u6784<\/strong><\/p>\n<ul>\n<li> <strong>Shebang \u884c\uff1a<\/strong>\u6307\u5b9a\u89e3\u91ca\u811a\u672c\u7684 Shell \u7c7b\u578b\uff0c\u5982\uff1a#!\/bin\/bash<\/li>\n<li> <strong>\u6ce8\u91ca\uff1a<\/strong>\u4f7f\u7528 &#8220;#&#8221; \u5f00\u5934\u7684\u884c\u7f16\u5199\u6ce8\u91ca\uff0c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/li>\n<li> <strong>\u53d8\u91cf\uff1a<\/strong>\u4f7f\u7528\u540d\u79f0\uff08\u4e0d\u5e26\u7a7a\u683c\uff09\u548c\u7b49\u53f7 (=) \u8d4b\u503c\uff0c\u5982\uff1amy_variable=value<\/li>\n<li> <strong>\u547d\u4ee4\uff1a<\/strong>\u7f16\u5199\u8981\u6267\u884c\u7684\u547d\u4ee4\uff0c\u5982\uff1aecho &#8220;Hello, world!&#8221;<\/li>\n<li> <strong>\u6761\u4ef6\u8bed\u53e5\uff1a<\/strong>\u7528\u4e8e\u6267\u884c\u57fa\u4e8e\u6761\u4ef6\u7684\u4ee3\u7801\uff0c\u5982 if \u548c else \u5757\u3002<\/li>\n<li> <strong>\u5faa\u73af\u8bed\u53e5\uff1a<\/strong>\u7528\u4e8e\u91cd\u590d\u6267\u884c\u4ee3\u7801\uff0c\u5982 for \u548c while \u5faa\u73af\u3002<\/li>\n<\/ul>\n<p><strong>4. \u57fa\u672c\u8bed\u6cd5<\/strong><\/p>\n<ul>\n<li> <strong>\u6253\u5370\u8f93\u51fa\uff1a<\/strong>\u4f7f\u7528 echo \u547d\u4ee4\uff0c\u5982\uff1aecho &#8220;Message to display&#8221;<\/li>\n<li> <strong>\u53d8\u91cf\u4f7f\u7528\uff1a<\/strong>\u4f7f\u7528 $ \u7b26\u53f7\u5f15\u7528\u53d8\u91cf\uff0c\u5982\uff1aecho $my_variable<\/li>\n<li> <strong>\u7ba1\u9053\uff1a<\/strong>\u4f7f\u7528\u5782\u76f4\u7ebf (|) \u5c06\u8f93\u51fa\u4ece\u4e00\u4e2a\u547d\u4ee4\u91cd\u5b9a\u5411\u5230\u53e6\u4e00\u4e2a\u547d\u4ee4\uff0c\u5982\uff1als | grep &#8220;file_name&#8221;<\/li>\n<li><strong>\u6761\u4ef6\u8bed\u53e5\uff1a<\/strong><\/li>\n<\/ul>\n<pre>if [ condition ]; then\n    # Code to execute if condition is true\nelse\n    # Code to execute if condition is false\nfi<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li><strong>\u5faa\u73af\u8bed\u53e5\uff1a<\/strong><\/li>\n<\/ul>\n<pre>for variable in list; do\n    # Code to execute for each item in the list\ndone<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u5b9e\u7528\u6280\u5de7<\/strong><\/p>\n<ul>\n<li> <strong>\u4f7f\u7528\u901a\u914d\u7b26\uff1a<\/strong>* \u5339\u914d\u4efb\u4f55\u6570\u91cf\u7684\u5b57\u7b26\uff0c? \u5339\u914d\u4efb\u4f55\u5355\u4e2a\u5b57\u7b26\u3002<\/li>\n<li> <strong>\u91cd\u5b9a\u5411\u8f93\u5165\/\u8f93\u51fa\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528 \u91cd\u5b9a\u5411\u8f93\u5165\u548c\u8f93\u51fa\uff0c\u5982\uff1a output.txt\u3002<\/li>\n<li> <strong>\u8c03\u8bd5\u811a\u672c\uff1a<\/strong>\u4f7f\u7528 set -x \u8c03\u8bd5\u811a\u672c\uff0c\u4ee5\u9010\u884c\u663e\u793a\u6267\u884c\u7684\u547d\u4ee4\u3002<\/li>\n<li> <strong>\u9519\u8bef\u5904\u7406\uff1a<\/strong>\u4f7f\u7528 exit \u547d\u4ee4\u5904\u7406\u9519\u8bef\uff0c\u5982\uff1aexit 1 \u8868\u793a\u9519\u8bef\u53d1\u751f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f&nbsp;\u600e\u4e48\u5199\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>linux shell \u811a\u672c\u7f16\u5199\u6307\u5357\u4e2d\u521b\u5efa\u811a\u672c\u7684\u5173\u952e\u6b65\u9aa4\u6709\uff1a1. \u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u4ee5 &#8220;.sh&#8221; \u6269\u5c55\u540d\u521b\u5efa\u65b0\u6587\u4ef6\uff1b2. \u4f7f\u7528 &#8220;chmod +x&#8221; \u8bbe\u7f6e\u53ef\u6267\u884c\u6743\u9650\u30023. shell \u811a\u672c\u7ed3\u6784\u5305\u542b shebang \u884c\u3001\u6ce8\u91ca\u3001\u53d8\u91cf\u3001\u547d\u4ee4\u3001\u6761\u4ef6\u548c\u5faa\u73af\u8bed\u53e5\u3002\u57fa\u672c\u8bed\u6cd5\u6d89\u53ca\u6253\u5370\u8f93\u51fa\u3001\u53d8\u91cf\u4f7f\u7528\u3001\u7ba1\u9053\u3001\u6761\u4ef6\u8bed\u53e5\u548c\u5faa\u73af\u8bed\u53e5\u3002\u5b9e\u7528\u6280\u5de7\u5305\u62ec\u4f7f\u7528\u901a\u914d\u7b26\u3001\u91cd\u5b9a\u5411\u8f93\u5165\/\u8f93\u51fa\u3001\u8c03\u8bd5\u548c\u9519\u8bef\u5904\u7406\u3002 Linux Shell \u811a\u672c\u7f16\u5199\u6307\u5357 1. \u521b\u5efa\u811a\u672c \u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982 nano \u6216 vim\uff09\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\uff0c\u5e76\u4ee5 &#8220;.sh&#8221; \u4f5c\u4e3a\u6269\u5c55\u540d\u3002 \u4f8b\u5982\uff1anano my_script.sh 2. \u8bbe\u7f6e\u53ef\u6267\u884c\u6743\u9650 \u5b8c\u6210\u811a\u672c\u7f16\u5199\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u53ef\u6267\u884c\u6743\u9650\uff1a chmod +x my_script.sh 3. Shell \u811a\u672c\u7ed3\u6784 Shebang \u884c\uff1a\u6307\u5b9a\u89e3\u91ca\u811a\u672c\u7684 Shell \u7c7b\u578b\uff0c\u5982\uff1a#!\/bin\/bash \u6ce8\u91ca\uff1a\u4f7f\u7528 &#8220;#&#8221; \u5f00\u5934\u7684\u884c\u7f16\u5199\u6ce8\u91ca\uff0c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002 \u53d8\u91cf\uff1a\u4f7f\u7528\u540d\u79f0\uff08\u4e0d\u5e26\u7a7a\u683c\uff09\u548c\u7b49\u53f7 (=) \u8d4b\u503c\uff0c\u5982\uff1amy_variable=value \u547d\u4ee4\uff1a\u7f16\u5199\u8981\u6267\u884c\u7684\u547d\u4ee4\uff0c\u5982\uff1aecho &#8220;Hello, world!&#8221; \u6761\u4ef6\u8bed\u53e5\uff1a\u7528\u4e8e\u6267\u884c\u57fa\u4e8e\u6761\u4ef6\u7684\u4ee3\u7801\uff0c\u5982 if \u548c else \u5757\u3002 \u5faa\u73af\u8bed\u53e5\uff1a\u7528\u4e8e\u91cd\u590d\u6267\u884c\u4ee3\u7801\uff0c\u5982 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-63861","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=63861"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63861\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=63861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=63861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=63861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}