You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package ExecUtil
import (
"dsTools/Utils/CommonUtil"
"bufio"
"fmt"
"io"
"os/exec"
"strings"
)
/**
功能执行SHELL命令并回显示结果
作者:黄海
时间2020-01-19
*/
func ExecCommand(commandName string, params []string) bool {
cmd := exec.Command(commandName, params...)
//显示运行的命令
fmt.Println(cmd.Args)
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
return false
}
cmd.Start()
reader := bufio.NewReader(stdout)
//实时循环读取输出流中的一行内容
for {
line, err2 := reader.ReadString('\n')
if err2 != nil || io.EOF == err2 {
break
}
fmt.Println(line)
}
cmd.Wait()
return true
}
/**
功能:执行一整行的命令
作者:黄海
时间2020-01-21
*/
func ExecCommandStr(commandName string){
//1、处理掉多余的空格
commandName= CommonUtil.DeleteExtraSpace(commandName)
//2、拆开
var arr=strings.Split(commandName," ")
//3、执行的命令
var cmd=arr[0]
//4、参数数组
params := arr[1:len(arr)]
//5、调用另一个实现
ExecCommand(cmd,params)
}