1 Star 0 Fork 0

ckbabby / goutil

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
exec.go 1.01 KB
一键复制 编辑 原始数据 按行查看 历史
ckbabby 提交于 2020-09-18 17:38 . 9-18
package sysutil
import (
"bytes"
"os/exec"
"gitee.com/ckbabby/goutil/strutil"
)
// QuickExec quick exec an simple command line
// Usage:
// QuickExec("git status")
func QuickExec(cmdLine string, workDir ...string) (string, error) {
ss := strutil.Split(cmdLine, " ")
return ExecCmd(ss[0], ss[1:], workDir...)
}
// ExecCmd an command and return output.
// Usage:
// ExecCmd("ls", []string{"-al"})
func ExecCmd(binName string, args []string, workDir ...string) (string, error) {
// create a new Cmd instance
cmd := exec.Command(binName, args...)
if len(workDir) > 0 {
cmd.Dir = workDir[0]
}
bs, err := cmd.Output()
return string(bs), err
}
// ShellExec exec command by shell
// cmdStr eg. "ls -al"
func ShellExec(cmdStr string, shells ...string) (string, error) {
// shell := "/bin/sh"
shell := "sh"
if len(shells) > 0 {
shell = shells[0]
}
var out bytes.Buffer
cmd := exec.Command(shell, "-c", cmdStr)
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return "", err
}
return out.String(), nil
}
Go
1
https://gitee.com/ckbabby/goutil.git
git@gitee.com:ckbabby/goutil.git
ckbabby
goutil
goutil
754e97ac523d

搜索帮助

53164aa7 5694891 3bd8fe86 5694891