Go实战--go语言中执行shell脚本(The way to go)
来源:互联网 发布:手机淘宝客软件 编辑:程序博客网 时间:2024/04/19 10:54
生命不止,继续go go go !!!
接触linux的人对shell一定不陌生,君不见那些噼里啪啦敲的飞快的服务端程序猿都是在键入,ls cd cat 等。
何为shell?
Simply put, the shell is a program that takes your commands from the keyboard and gives them to the operating system to perform. In the old days, it was the only user interface available on a Unix computer. Nowadays, we have graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell.
On most Linux systems a program called bash (which stands for Bourne Again SHell, an enhanced version of the original Bourne shell program, sh, written by Steve Bourne) acts as the shell program. There are several additional shell programs available on a typical Linux system. These include: ksh, tcsh and zsh.
Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁。Shell既是一种命令语言,又是一种程序设计语言。
Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。
os/exec package
go中为我们提供了一个os/exec包:用于运行外部的命令。
Cmd结构
type Cmd struct { Path string Args []string Env []string Dir string Stdin io.Reader Stdout io.Writer Stderr io.Writer ExtraFiles []*os.File SysProcAttr *syscall.SysProcAttr Process *os.Process ProcessState *os.ProcessState}
其中, Stdout specify the process’s standard output
func Command
原型:
func Command(name string, arg ...string) *Cmd
作用:Command returns the Cmd struct to execute the named program with the given arguments.
例如:
cmd := exec.Command(“/bin/bash”, “-c”, “ls”)
cmd := exec.Command(“tr”, “a-z”, “A-Z”)
func (*Cmd) Run
func (c *Cmd) Output() ([]byte, error)
作用:开始运行并等待结束。
这里需要注意Run和Start的区别:
Start starts the specified command but does not wait for it to complete.
开始运行不等待结束。
应用代码
package mainimport ( "bytes" "fmt" "log" "os/exec" )func exec_shell(s string) { cmd := exec.Command("/bin/bash", "-c", s) var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { log.Fatal(err) } fmt.Printf("%s", out.String())}func main() { exec_shell("uname ")}
输出:
Linux
应用代码2
package mainimport ( "fmt" "os/exec" "sync" "strings")func exe_cmd(cmd string, wg *sync.WaitGroup) { fmt.Println(cmd) parts := strings.Fields(cmd) out, err := exec.Command(parts[0],parts[1]).Output() if err != nil { fmt.Println("error occured") fmt.Printf("%s", err) } fmt.Printf("%s", out) wg.Done()}func main() { wg := new(sync.WaitGroup) commands := []string{"echo newline >> foo.o", "echo newline >> f1.o", "echo newline >> f2.o"} for _, str := range commands { wg.Add(1) go exe_cmd(str, wg) } wg.Wait()}
这段代码用到了sync包,之后会介绍的。
- Go实战--go语言中执行shell脚本(The way to go)
- Go实战--go中编码转换(The way to go)
- Go实战--go中使用libphonenumber(The way to go)
- Go实战--go中使用cookie(The way to go)
- Go实战--go中使用rpc(The way to go)
- Go实战--go语言操作sqlite数据库(The way to go)
- Go实战--go中使用google/protobuf(The way to go)
- Go实战--go中使用base64加密(The way to go)
- Go实战--go中使用hmac sha256(The way to go)
- Go实战--go中一些有用的代码片段(The way to go)
- Go实战--go中函数(function)和方法(method)的使用(The way to go)
- Go实战--go中函数递归(recursion)的使用(The way to go)
- The way to go !
- Go实战--golang新手入门常见错误(The way to go)
- Go实战--golang生成uuid(The way to go)
- Go语言学习之Hello World(The way to go)
- Go语言学习之变量(The way to go)
- Go语言学习之常量(The way to go)
- c实现的算法
- 程序的本质复杂性和元语言抽象
- 对象
- lightoj-1029-Civil and Evil Engineer(最小生成树+克鲁斯卡尔算法)
- js 和ajax 提交给服务器端的代码, 验证输入值符不符合,验证服务器传递过来的值 thinkphp
- Go实战--go语言中执行shell脚本(The way to go)
- Servlet知识点
- 强化学习进阶 第七讲 TRPO
- java中四种引用类型(对象的强、软、弱和虚引用)
- Android Adapter,UI界面更新
- C++实现简单的走迷宫
- CodeForces 625 D.Finals in arithmetic(构造)
- bootstrap编写响应式页面
- 对象2