{"id":18109,"date":"2024-11-18T08:52:57","date_gmt":"2024-11-18T00:52:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/18109\/"},"modified":"2024-11-18T08:52:57","modified_gmt":"2024-11-18T00:52:57","slug":"linux%e6%80%8e%e4%b9%88%e7%bc%96%e5%86%99shell%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/18109\/","title":{"rendered":"linux\u600e\u4e48\u7f16\u5199shell\u811a\u672c"},"content":{"rendered":"<blockquote><p>\n  \u7f16\u5199 linux shell \u811a\u672c\u5206\u5341\u6b65\uff1a\u9009\u62e9\u811a\u672c\u7f16\u8f91\u5668\u5e76\u4f7f\u7528 .sh \u6269\u5c55\u540d\u521b\u5efa\u811a\u672c\u3002\u6307\u5b9a shebang \u884c\u4ee5\u6307\u5b9a\u6240\u7528 shell\uff08\u4f8b\u5982 #!\/bin\/bash\uff09\u3002\u4f7f\u7528 = \u8fd0\u7b97\u7b26\u5b9a\u4e49\u53d8\u91cf\uff08\u4f8b\u5982 my_name=&#8221;john doe&#8221;\uff09\u3002\u4f7f\u7528 # \u6ce8\u91ca\u7b26\u6dfb\u52a0\u6ce8\u91ca\u4ee5\u89e3\u91ca\u4ee3\u7801\u3002\u4f7f\u7528 shell \u547d\u4ee4\u6267\u884c\u64cd\u4f5c\uff08\u5185\u7f6e\u6216\u5916\u90e8\uff09\u3002\u4f7f\u7528 if\u3001else \u548c for \u7b49\u8bed\u53e5\u63a7\u5236\u6d41\u7a0b\u3002\u5c06\u4ee3\u7801\u5757\u5206\u7ec4\u5230\u51fd\u6570\u4e2d\u4ee5\u5b9e\u73b0\u91cd\u7528\u548c\u6a21\u5757\u5316\u3002\u4f7f\u7528 bash -x script.sh \u547d\u4ee4\u9010\u6b65\u8c03\u8bd5\u811a\u672c\u3002\u4f7f\u7528 chmod +x sc\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060312062262043.jpg\" class=\"aligncenter\" title=\"linux\u600e\u4e48\u7f16\u5199shell\u811a\u672c\u63d2\u56fe\" alt=\"linux\u600e\u4e48\u7f16\u5199shell\u811a\u672c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199 Linux Shell \u811a\u672c<\/strong><\/p>\n<p>Shell \u811a\u672c\u662f\u81ea\u52a8\u5316 Linux \u4efb\u52a1\u7684\u5f3a\u5927\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u7f16\u5199 shell \u811a\u672c\u7684\u5206\u6b65\u6307\u5357\uff1a<\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u9009\u62e9\u811a\u672c\u7f16\u8f91\u5668<\/strong><\/p>\n<p>\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\uff08\u4f8b\u5982 nano\u3001vi \u6216 gedit\uff09\u521b\u5efa shell \u811a\u672c\u3002\u786e\u4fdd\u6587\u4ef6\u6269\u5c55\u540d\u4e3a .sh\u3002<\/p>\n<p><strong>\u6b65\u9aa4 2\uff1a\u7f16\u5199 Shebang \u884c<\/strong><\/p>\n<p>\u811a\u672c\u7684\u7b2c\u4e00\u884c\u5fc5\u987b\u6307\u5b9a\u6240\u7528 shell\u3002\u5bf9\u4e8e Bash shell\uff0c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>#!\/bin\/bash<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 3\uff1a\u5b9a\u4e49\u53d8\u91cf<\/strong><\/p>\n<p>\u4f7f\u7528 = \u8fd0\u7b97\u7b26\u5c06\u503c\u5206\u914d\u7ed9\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>my_name=\"John Doe\"\nage=30<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 4\uff1a\u6dfb\u52a0\u6ce8\u91ca<\/strong><\/p>\n<p>\u4f7f\u7528 # \u6ce8\u91ca\u7b26\u6dfb\u52a0\u6ce8\u91ca\u4ee5\u89e3\u91ca\u811a\u672c\u4e2d\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<pre># This line prints the welcome message<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 5\uff1a\u521b\u5efa\u547d\u4ee4<\/strong><\/p>\n<p>\u4f7f\u7528 shell \u5185\u7f6e\u547d\u4ee4\u6216\u5916\u90e8\u547d\u4ee4\u6267\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>echo \"Hello, world!\"    # \u5185\u7f6e\u547d\u4ee4\nls -l                # \u5916\u90e8\u547d\u4ee4<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 6\uff1a\u63a7\u5236\u6d41<\/strong><\/p>\n<p>\u4f7f\u7528 if\u3001else \u548c for \u7b49\u8bed\u53e5\u63a7\u5236\u811a\u672c\u4e2d\u7684\u6d41\u7a0b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>if [ \"$age\" -gt 18 ]; then\n  echo \"You are an adult.\"\nelse\n  echo \"You are a minor.\"\nfi<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 7\uff1a\u51fd\u6570<\/strong><\/p>\n<p>\u5c06\u4ee3\u7801\u5757\u5206\u7ec4\u5230\u51fd\u6570\u4e2d\u4ee5\u91cd\u7528\u548c\u6a21\u5757\u5316\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>my_function() {\n  echo \"This is a function.\"\n}\nmy_function<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 8\uff1a\u8c03\u8bd5\u811a\u672c<\/strong><\/p>\n<p>\u4f7f\u7528 bash -x script.sh \u547d\u4ee4\u9010\u6b65\u8c03\u8bd5\u811a\u672c\u3002\u8fd9\u5c06\u6253\u5370\u6bcf\u884c\u547d\u4ee4\u53ca\u5176\u8f93\u51fa\u3002<\/p>\n<p><strong>\u6b65\u9aa4 9\uff1a\u4f7f\u811a\u672c\u53ef\u6267\u884c<\/strong><\/p>\n<p>\u4f7f\u7528 chmod +x script.sh \u547d\u4ee4\u4f7f\u811a\u672c\u53ef\u6267\u884c\u3002<\/p>\n<p><strong>\u6b65\u9aa4 10\uff1a\u8fd0\u884c\u811a\u672c<\/strong><\/p>\n<p>\u4f7f\u7528 .\/script.sh \u547d\u4ee4\u8fd0\u884c\u811a\u672c\u3002\u901a\u8fc7\u6307\u5b9a\u8f93\u5165\u6216\u8f93\u51fa\u91cd\u5b9a\u5411\uff0c\u53ef\u4ee5\u4ece\u547d\u4ee4\u884c\u4f20\u9012\u53c2\u6570\u6216\u91cd\u5b9a\u5411\u8f93\u51fa\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u7f16\u5199\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u5199 linux shell \u811a\u672c\u5206\u5341\u6b65\uff1a\u9009\u62e9\u811a\u672c\u7f16\u8f91\u5668\u5e76\u4f7f\u7528 .sh \u6269\u5c55\u540d\u521b\u5efa\u811a\u672c\u3002\u6307\u5b9a shebang \u884c\u4ee5\u6307\u5b9a\u6240\u7528 shell\uff08\u4f8b\u5982 #!\/bin\/bash\uff09\u3002\u4f7f\u7528 = \u8fd0\u7b97\u7b26\u5b9a\u4e49\u53d8\u91cf\uff08\u4f8b\u5982 my_name=&#8221;john doe&#8221;\uff09\u3002\u4f7f\u7528 # \u6ce8\u91ca\u7b26\u6dfb\u52a0\u6ce8\u91ca\u4ee5\u89e3\u91ca\u4ee3\u7801\u3002\u4f7f\u7528 shell \u547d\u4ee4\u6267\u884c\u64cd\u4f5c\uff08\u5185\u7f6e\u6216\u5916\u90e8\uff09\u3002\u4f7f\u7528 if\u3001else \u548c for \u7b49\u8bed\u53e5\u63a7\u5236\u6d41\u7a0b\u3002\u5c06\u4ee3\u7801\u5757\u5206\u7ec4\u5230\u51fd\u6570\u4e2d\u4ee5\u5b9e\u73b0\u91cd\u7528\u548c\u6a21\u5757\u5316\u3002\u4f7f\u7528 bash -x script.sh \u547d\u4ee4\u9010\u6b65\u8c03\u8bd5\u811a\u672c\u3002\u4f7f\u7528 chmod +x sc \u5982\u4f55\u7f16\u5199 Linux Shell \u811a\u672c Shell \u811a\u672c\u662f\u81ea\u52a8\u5316 Linux \u4efb\u52a1\u7684\u5f3a\u5927\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u7f16\u5199 shell \u811a\u672c\u7684\u5206\u6b65\u6307\u5357\uff1a \u6b65\u9aa4 1\uff1a\u9009\u62e9\u811a\u672c\u7f16\u8f91\u5668 \u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\uff08\u4f8b\u5982 nano\u3001vi \u6216 gedit\uff09\u521b\u5efa shell \u811a\u672c\u3002\u786e\u4fdd\u6587\u4ef6\u6269\u5c55\u540d\u4e3a .sh\u3002 \u6b65\u9aa4 2\uff1a\u7f16\u5199 Shebang \u884c \u811a\u672c\u7684\u7b2c\u4e00\u884c\u5fc5\u987b\u6307\u5b9a\u6240\u7528 shell\u3002\u5bf9\u4e8e Bash [&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-18109","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18109","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=18109"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18109\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=18109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=18109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=18109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}