From f2d03c417e3eeb47e16a4d0eea2bdc08a3c9c818 Mon Sep 17 00:00:00 2001 From: john Date: Fri, 24 Aug 2018 17:11:50 +0800 Subject: [PATCH] =?UTF-8?q?gproc=E5=A2=9E=E5=8A=A0=E6=89=A7=E8=A1=8Cshell?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gproc/gproc.go | 80 ++++++++++++++++++++++++++++++++++++ g/os/gproc/gproc_proccess.go | 15 ++++--- geg/os/gproc/gproc5.go | 13 ++---- geg/other/test.go | 11 ++--- 4 files changed, 97 insertions(+), 22 deletions(-) diff --git a/g/os/gproc/gproc.go b/g/os/gproc/gproc.go index 8fd029d11..71265099b 100644 --- a/g/os/gproc/gproc.go +++ b/g/os/gproc/gproc.go @@ -14,6 +14,10 @@ import ( "time" "gitee.com/johng/gf/g/util/gconv" "strings" + "bytes" + "io" + "runtime" + "gitee.com/johng/gf/g/os/gfile" ) const ( @@ -72,6 +76,29 @@ func Uptime() int { return int(time.Now().UnixNano()/1e6 - processStartTime.UnixNano()/1e6) } +// 阻塞执行shell指令,并给定输入输出对象 +func Shell(cmd string, out io.Writer, in io.Reader) error { + p := NewProcess(getShell(), []string{getShellOption(), cmd}) + p.Stdin = in + p.Stdout = out + return p.Run() +} + +// 阻塞执行shell指令,并输出结果当终端(如果需要异步,请使用goroutine) +func ShellRun(cmd string) error { + p := NewProcess(getShell(), []string{getShellOption(), cmd}) + return p.Run() +} + +// 阻塞执行shell指令,并返回输出结果(如果需要异步,请使用goroutine) +func ShellExec(cmd string) (string, error) { + buf := bytes.NewBuffer(nil) + p := NewProcess(getShell(), []string{getShellOption(), cmd}) + p.Stdout = buf + err := p.Run() + return buf.String(), err +} + // 检测环境变量中是否已经存在指定键名 func checkEnvKey(env []string, key string) bool { for _, v := range env { @@ -82,3 +109,56 @@ func checkEnvKey(env []string, key string) bool { return false } +// 获取当前系统下的shell路径 +func getShell() string { + switch runtime.GOOS { + case "windows": + return searchBinFromEnvPath("cmd.exe") + default: + path := searchBinFromEnvPath("bash") + if path == "" { + path = searchBinFromEnvPath("sh") + } + return path + } + return "" +} + +// 获取当前系统默认shell执行指令的option参数 +func getShellOption() string { + switch runtime.GOOS { + case "windows": + return "/c" + default: + return "-c" + } + return "" +} + +// 从环境变量PATH中搜索可执行文件 +func searchBinFromEnvPath(file string) string { + // 如果是绝对路径,或者相对路径下存在,那么直接返回 + if gfile.Exists(file) { + return file + } + array := ([]string)(nil) + switch runtime.GOOS { + case "windows": + array = strings.Split(os.Getenv("Path"), ";") + if gfile.Ext(file) != "exe" { + file += ".exe" + } + default: + array = strings.Split(os.Getenv("PATH"), ":") + } + if len(array) > 0 { + for _, v := range array { + path := v + gfile.Separator + file + if gfile.Exists(path) { + return path + } + } + } + return "" +} + diff --git a/g/os/gproc/gproc_proccess.go b/g/os/gproc/gproc_proccess.go index 7991b3c8a..9884eb800 100644 --- a/g/os/gproc/gproc_proccess.go +++ b/g/os/gproc/gproc_proccess.go @@ -22,12 +22,17 @@ type Process struct { } // 创建一个进程(不执行) -func NewProcess(path string, args []string, environment []string) *Process { - env := make([]string, len(environment) + 1) - for k, v := range environment { - env[k] = v +func NewProcess(path string, args []string, environment...[]string) *Process { + var env []string + if len(environment) > 0 { + env = make([]string, 0) + for _, v := range environment[0] { + env = append(env, v) + } + } else { + env = os.Environ() } - env[len(env) - 1] = fmt.Sprintf("%s=%s", gPROC_TEMP_DIR_ENV_KEY, os.TempDir()) + env= append(env, fmt.Sprintf("%s=%s", gPROC_TEMP_DIR_ENV_KEY, os.TempDir())) p := &Process { Manager : nil, PPid : os.Getpid(), diff --git a/geg/os/gproc/gproc5.go b/geg/os/gproc/gproc5.go index db5eddf12..b83bbe54a 100644 --- a/geg/os/gproc/gproc5.go +++ b/geg/os/gproc/gproc5.go @@ -1,17 +1,12 @@ package main import ( - "os" - "time" - "gitee.com/johng/gf/g/os/glog" - "gitee.com/johng/gf/g/os/genv" "gitee.com/johng/gf/g/os/gproc" + "fmt" ) -// 查看进程的环境变量 +// 执行shell指令 func main () { - time.Sleep(5*time.Second) - glog.Printfln("%d: %v", gproc.Pid(), genv.All()) - p := gproc.NewProcess(os.Args[0], os.Args, os.Environ()) - p.Start() + r, err := gproc.ShellExec("echo 'hello';") + fmt.Println("result:", r, err) } diff --git a/geg/other/test.go b/geg/other/test.go index 8a734dcc6..ceaafd29e 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,15 +1,10 @@ package main import ( - "gitee.com/johng/gf/g/util/gregex" - "fmt" + "gitee.com/johng/gf/g/util/gutil" + "gitee.com/johng/gf/g/os/genv" ) - func main() { - s := `百度"` - gregex.ReplaceStringFunc(`href="(.+?)"`, s, func(s string) string { - fmt.Println(s) - return s - }) + gutil.Dump(genv.All()) } \ No newline at end of file