<\/span><\/h1>\n\n\n\nos\/exec<\/strong> package can be used to trigger any OS command from Go. The same can be used for triggering .sh file. <\/p>\n\n\n\nFirst, create a sample.sh file in the same directory. Make sure it either starts with #!\/bin\/sh or #!\/bin\/bash<\/li><\/ul>\n\n\n\nsample.sh<\/strong><\/p>\n\n\n\n#!\/bin\/sh\necho \"Triggered from .go file\" > out.out<\/code><\/pre>\n\n\n\nMake this sample.sh file executable<\/li><\/ul>\n\n\n\nchmod +x sample.sh<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
<\/span>Code:<\/strong><\/span><\/h1>\n\n\n\nCreate a below main.go<\/strong> file in the same directory<\/p>\n\n\n\npackage main\n\nimport (\n \"fmt\"\n \"log\"\n \"os\/exec\"\n)\n\nfunc main() {\n out, err := exec.Command(\"\/bin\/sh\", \"sample.sh\").Output()\n if err != nil {\n log.Fatal(err)\n }\n fmt.Println(out)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nA out.out file will be created in the same directory<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Table of Contents Overview:Code: Overview: os\/exec package can be used to trigger any OS command from Go. The same can be used for triggering .sh file. First, create a sample.sh file in…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[197],"class_list":["post-1419","post","type-post","status-publish","format-standard","hentry","category-tech","tag-bash"],"yoast_head":"\n
Execute shell file from Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n