{"id":206591,"date":"2025-06-12T08:47:01","date_gmt":"2025-06-12T00:47:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206591\/"},"modified":"2025-06-12T08:47:01","modified_gmt":"2025-06-12T00:47:01","slug":"golang-exec-command-%e5%90%8e%e5%8f%b0%e5%ae%88%e6%8a%a4%e6%89%a7%e8%a1%8c-shell-%e5%91%bd%e4%bb%a4%e6%97%b6%ef%bc%8c%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%91%bd%e4%bb%a4%e6%89%a7%e8%a1%8c%e7%8a%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206591\/","title":{"rendered":"Golang exec.Command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u5982\u4f55\u83b7\u53d6\u547d\u4ee4\u6267\u884c\u72b6\u6001\u5e76\u5904\u7406\u9519\u8bef\uff1f"},"content":{"rendered":"<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGOLANG exec.Command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u5982\u4f55\u83b7\u53d6\u547d\u4ee4\u6267\u884c\u72b6\u6001\u5e76\u5904\u7406\u9519\u8bef\uff1f\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span>,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/173174530167385615ef363.jpg\" class=\"aligncenter\">,<br \/>\n<strong>golang exec.command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u95ee\u9898<\/strong>,\u5728\u4f7f\u7528 exec.command \u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u4f5c\u8005\u5728\u540e\u53f0\u5b88\u62a4\u4e0b\u9047\u5230\u4e86\u95ee\u9898\uff0c\u547d\u4ee4\u4f3c\u4e4e\u65e0\u6cd5\u6b63\u5e38\u6267\u884c\u3002,<br \/>\n<strong>\u95ee\u9898\u539f\u56e0<\/strong>,\u95ee\u9898\u662f\u7531\u4e8e\u4f5c\u8005\u76f4\u63a5\u4f7f\u7528 cmd.run() \u65b9\u6cd5\u542f\u52a8\u547d\u4ee4\u3002\u867d\u7136\u7406\u8bba\u4e0a cmd.run() \u4f1a\u963b\u585e\u5e76\u7b49\u5f85\u547d\u4ee4\u6267\u884c\u5b8c\u6bd5\uff0c\u4f46\u5728\u540e\u53f0\u5b88\u62a4\u4e0b\u8fd9\u4e2a\u673a\u5236\u53ef\u80fd\u5931\u6548\uff0c\u5bfc\u81f4\u547d\u4ee4\u6267\u884c\u540e\u7acb\u5373\u7ed3\u675f\u5b88\u62a4\u8fdb\u7a0b\u3002,<br \/>\n<strong>\u89e3\u51b3\u65b9\u6848<\/strong>,\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 stdoutpipe() \u548c stderrpipe() \u65b9\u6cd5\u83b7\u53d6\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u76d1\u63a7\u547d\u4ee4\u7684\u6267\u884c\u72b6\u6001\uff0c\u5e76\u4ece\u8f93\u51fa\u4e2d\u83b7\u53d6\u8fd4\u56de\u7801\u6216\u5176\u4ed6\u6267\u884c\u4fe1\u606f\u3002,\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a,\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f5c\u8005\u53ef\u4ee5\u5728\u540e\u53f0\u5b88\u62a4\u4e0b\u76d1\u63a7\u547d\u4ee4\u7684\u6267\u884c\u72b6\u6001\uff0c\u5e76\u5904\u7406\u76f8\u5e94\u7684\u9519\u8bef\u6216\u8f93\u51fa\u3002,\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aGOLANG exec.Command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u5982\u4f55\u83b7\u53d6\u547d\u4ee4\u6267\u884c\u72b6\u6001\u5e76\u5904\u7406\u9519\u8bef\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u5427\uff01,golang exec.command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u95ee\u9898,<br \/>\n<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGOLANG exec.Command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u5982\u4f55\u83b7\u53d6\u547d\u4ee4\u6267\u884c\u72b6\u6001\u5e76\u5904\u7406\u9519\u8bef\uff1f\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span>,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>GOLANG exec.Command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u5982\u4f55\u83b7\u53d6\u547d\u4ee4\u6267\u884c\u72b6\u6001\u5e76\u5904\u7406\u9519\u8bef\uff1f<\/span><\/p>\n<h1>GOLANG exec.Command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u5982\u4f55\u83b7\u53d6\u547d\u4ee4\u6267\u884c\u72b6\u6001\u5e76\u5904\u7406\u9519\u8bef\uff1f<\/h1>\n<p><span>2024-11-16 16:21:57<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGOLANG exec.Command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u5982\u4f55\u83b7\u53d6\u547d\u4ee4\u6267\u884c\u72b6\u6001\u5e76\u5904\u7406\u9519\u8bef\uff1f\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/173174530167385615ef363.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>golang exec.command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u95ee\u9898<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 exec.command \u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u4f5c\u8005\u5728\u540e\u53f0\u5b88\u62a4\u4e0b\u9047\u5230\u4e86\u95ee\u9898\uff0c\u547d\u4ee4\u4f3c\u4e4e\u65e0\u6cd5\u6b63\u5e38\u6267\u884c\u3002<\/p>\n<p><strong>\u95ee\u9898\u539f\u56e0<\/strong><\/p>\n<p>\u95ee\u9898\u662f\u7531\u4e8e\u4f5c\u8005\u76f4\u63a5\u4f7f\u7528 cmd.run() \u65b9\u6cd5\u542f\u52a8\u547d\u4ee4\u3002\u867d\u7136\u7406\u8bba\u4e0a cmd.run() \u4f1a\u963b\u585e\u5e76\u7b49\u5f85\u547d\u4ee4\u6267\u884c\u5b8c\u6bd5\uff0c\u4f46\u5728\u540e\u53f0\u5b88\u62a4\u4e0b\u8fd9\u4e2a\u673a\u5236\u53ef\u80fd\u5931\u6548\uff0c\u5bfc\u81f4\u547d\u4ee4\u6267\u884c\u540e\u7acb\u5373\u7ed3\u675f\u5b88\u62a4\u8fdb\u7a0b\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 stdoutpipe() \u548c stderrpipe() \u65b9\u6cd5\u83b7\u53d6\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u76d1\u63a7\u547d\u4ee4\u7684\u6267\u884c\u72b6\u6001\uff0c\u5e76\u4ece\u8f93\u51fa\u4e2d\u83b7\u53d6\u8fd4\u56de\u7801\u6216\u5176\u4ed6\u6267\u884c\u4fe1\u606f\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre>output := exec.Command(\"ls\")\nstdout, err := output.StdoutPipe()\nif err != nil {\n    \/\/ \u5904\u7406\u9519\u8bef\n}\n_ = output.Start()\n\n\/\/ \u83b7\u53d6\u547d\u4ee4\u8f93\u51fa\u5e76\u68c0\u67e5\u8fd4\u56de\u7801\nvar code int\nif buf, err := ioutil.ReadAll(stdout); err == nil {\n    fmt.Println(string(buf))\n} else {\n    fmt.Println(err)\n}\n_ = output.Wait()\nif err := output.Wait(); err != nil {\n    if exitError, ok := err.(*exec.ExitError); ok {\n        code = exitError.ExitCode()\n    } else {\n        \/\/ \u5904\u7406\u5176\u4ed6\u9519\u8bef\u7c7b\u578b\n    }\n}<\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f5c\u8005\u53ef\u4ee5\u5728\u540e\u53f0\u5b88\u62a4\u4e0b\u76d1\u63a7\u547d\u4ee4\u7684\u6267\u884c\u72b6\u6001\uff0c\u5e76\u5904\u7406\u76f8\u5e94\u7684\u9519\u8bef\u6216\u8f93\u51fa\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aGOLANG exec.Command \u540e\u53f0\u5b88\u62a4\u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u5982\u4f55\u83b7\u53d6\u547d\u4ee4\u6267\u884c\u72b6\u6001\u5e76\u5904\u7406\u9519\u8bef\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u6dd8\u5b9dapp\u600e\u4e48\u4fee\u6539\u5bc6\u7801-\u6dd8\u5b9dapp\u4fee\u6539\u5bc6\u7801\u7684\u65b9\u6cd5\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   win11\u600e\u4e48\u8bbe\u7f6e\u589e\u5f3a\u97f3\u9891 win11\u6253\u5f00\u589e\u5f3a\u97f3\u9891\u65b9\u6cd5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-206591","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=206591"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206591\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}