{"id":125008,"date":"2018-10-24T10:40:08","date_gmt":"2018-10-24T02:40:08","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=125008"},"modified":"2018-10-22T09:41:08","modified_gmt":"2018-10-22T01:41:08","slug":"golang-exec-command","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/golang-exec-command.html","title":{"rendered":"\u4ecb\u7ecd golang\u6267\u884c\u7cfb\u7edf\u547d\u4ee4"},"content":{"rendered":"

golang\u4e0b\u7684os\/exec\u5305\u6267\u884c\u5916\u90e8\u547d\u4ee4\uff0c\u5b83\u5c06os.StartProcess\u8fdb\u884c\u5305\u88c5\u4f7f\u5f97\u5b83\u66f4\u5bb9\u6613\u6620\u5c04\u5230stdin\u548cstdout\u3002\u8fd9\u70b9\u548cpython\u4e0b\u7684command\u3001os.system\u7b49\u529f\u80fd\u662f\u4e00\u6837\u7684\u3002\u8fd9\u91cc\u5217\u4e24\u4e2a\u5177\u4f53\u64cd\u4f5c\u7684\u4ee3\u7801\u3002\u5176\u53ef\u4ee5\u8c03\u7528\u7c7bLINUX\u7cfb\u7edf\u4e0b\u7684shell\u547d\u4ee4\uff0c\u4e5f\u53ef\u4ee5\u5728windows\u4e0b\u8c03\u7528cmd\u4e0b\u7684\u547d\u4ee4\u3002<\/p>\n

\u4ee3\u7801\u4e00<\/strong><\/span><\/div>\n
package main\r\nimport (\r\n\"bytes\"\r\n\"fmt\"\r\n\"log\"\r\n\"os\/exec\"\r\n)\r\nconst ShellToUse = \"bash\"\r\nfunc Shellout(command string) (error, string, string) {\r\nvar stdout bytes.Buffer\r\nvar stderr bytes.Buffer\r\ncmd := exec.Command(ShellToUse, \"-c\", command)\r\ncmd.Stdout = &stdout\r\ncmd.Stderr = &stderr\r\nerr := cmd.Run()\r\nreturn err, stdout.String(), stderr.String()\r\n}\r\nfunc main() {\r\nerr, out, errout := Shellout(\"ls -ltr\")\r\nif err != nil {\r\nlog.Printf(\"error: %v\\n\", err)\r\n}\r\nfmt.Println(\"--- stdout ---\")\r\nfmt.Println(out)\r\nfmt.Println(\"--- stderr ---\")\r\nfmt.Println(errout)\r\n}\r\n<\/pre>\n

\u4ee5\u4e0a\u4ee3\u7801\u6267\u884c\u540e\uff0c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n

[root@361way go] go run cmd.go\r\n--- stdout ---\r\ntotal 24\r\ndrwxr-xr-x 6 root root 79 Jun 13 2017 src\r\n-rw-r--r-- 1 root root 752 Aug 19 10:55 login.go\r\n-rw-r--r-- 1 root root 1189 Aug 19 14:29 log.go\r\n-rw-r--r-- 1 root root 623 Aug 19 16:43 cmd.go\r\n-rw-r--r-- 1 root root 538 Aug 19 17:41 cmd2.go\r\n-rw-r--r-- 1 root root 519 Aug 19 21:38 http.go\r\n-rw-r--r-- 1 root root 462 Aug 19 22:29 test.py\r\n--- stderr ---\r\n<\/pre>\n
\u4ee3\u7801\u4e8c<\/strong><\/span><\/div>\n
package main\r\nimport (\r\n\"fmt\"\r\n\"os\/exec\"\r\n)\r\nfunc Cmd(cmd string, shell bool) []byte {\r\nif shell {\r\nout, err := exec.Command(\"bash\", \"-c\", cmd).Output()\r\nif err != nil {\r\npanic(\"some error found\")\r\n}\r\nreturn out\r\n} else {\r\nout, err := exec.Command(cmd).Output()\r\nif err != nil {\r\npanic(\"some error found\")\r\n}\r\nreturn out\r\n}\r\n}\r\nfunc main() {\r\n\/\/cmd := \"ls -al\"\r\ncmd := \"python test.py\"\r\n\/\/cmd := \"python -V\" \/\/\u6ca1\u6709\u8f93\u51fa\r\n\/\/cmd := \"env\"\r\nout := string(Cmd(cmd,true))\r\n\/\/out := string(Cmd(cmd,false))\r\nfmt.Println(out)\r\n}\r\n<\/pre>\n

\u56e0\u4e3a\u90fd\u662f\u8c03\u7528\u7684exec.Command\u65b9\u6cd5\u5e76\u8c03\u7528bash shell\uff0c\u672c\u8d28\u4e0a\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\u3002\u5176\u4e5f\u53ef\u4ee5\u6b63\u5e38\u8c03\u7528python\u811a\u672c\uff0c\u4f46\u5728\u76f4\u63a5\u8c03\u7528python -V\u547d\u4ee4\u65f6\u6ca1\u6709\u8f93\u51fa\uff0c\u8fd9\u70b9\u611f\u89c9\u5f88\u5947\u602a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

golang\u4e0b\u7684os\/exec\u5305\u6267\u884c\u5916\u90e8\u547d\u4ee4\uff0c\u5b83\u5c06os.StartProcess\u8fdb\u884c\u5305\u88c5\u4f7f\u5f97\u5b83\u66f4\u5bb9\u6613\u6620\u5c04\u5230st […]<\/p>\n","protected":false},"author":643,"featured_media":125019,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-125008","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/125008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=125008"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/125008\/revisions"}],"predecessor-version":[{"id":125076,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/125008\/revisions\/125076"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/125019"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=125008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=125008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=125008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}