mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
doc: update doc for ExecCommand
This commit is contained in:
@@ -211,7 +211,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="ExecCommand">CompareOsEnv</span>
|
### <span id="ExecCommand">CompareOsEnv</span>
|
||||||
<p>Use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p>
|
<p>Execute shell command, return the stdout and stderr string of command, and error if error occur. param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1. In linux, use /bin/bash -c to execute command, In windows, use powershell.exe to execute command.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -227,10 +227,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
out, errout, err := system.ExecCommand("ls")
|
// linux or mac
|
||||||
fmt.Println(out)
|
stdout, stderr, err := system.ExecCommand("ls")
|
||||||
fmt.Println(errout)
|
fmt.Println("std out: ", stdout)
|
||||||
fmt.Println(err)
|
fmt.Println("std err: ", stderr)
|
||||||
|
assert.Equal("", stderr)
|
||||||
|
|
||||||
|
// windows
|
||||||
|
stdout, stderr, err = system.ExecCommand("dir")
|
||||||
|
fmt.Println("std out: ", stdout)
|
||||||
|
fmt.Println("std err: ", stderr)
|
||||||
|
|
||||||
|
// error command
|
||||||
|
stdout, stderr, err = system.ExecCommand("abc")
|
||||||
|
fmt.Println("std out: ", stdout)
|
||||||
|
fmt.Println("std err: ", stderr)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -212,7 +212,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="ExecCommand">ExecCommand</span>
|
### <span id="ExecCommand">ExecCommand</span>
|
||||||
<p>使用shell /bin/bash -c(linux) 或 cmd (windows) 执行shell命令</p>
|
<p>执行shell命令,返回命令的stdout和stderr字符串,如果出现错误,则返回错误。参数`command`是一个完整的命令字符串,如ls-a(linux),dir(windows),ping 127.0.0.1。在linux中,使用/bin/bash-c执行命令,在windows中,使用powershell.exe执行命令。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -228,10 +228,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
out, errout, err := system.ExecCommand("ls")
|
// linux or mac
|
||||||
fmt.Println(out)
|
stdout, stderr, err := system.ExecCommand("ls")
|
||||||
fmt.Println(errout)
|
fmt.Println("std out: ", stdout)
|
||||||
fmt.Println(err)
|
fmt.Println("std err: ", stderr)
|
||||||
|
assert.Equal("", stderr)
|
||||||
|
|
||||||
|
// windows
|
||||||
|
stdout, stderr, err = system.ExecCommand("dir")
|
||||||
|
fmt.Println("std out: ", stdout)
|
||||||
|
fmt.Println("std err: ", stderr)
|
||||||
|
|
||||||
|
// error command
|
||||||
|
stdout, stderr, err = system.ExecCommand("abc")
|
||||||
|
fmt.Println("std out: ", stdout)
|
||||||
|
fmt.Println("std err: ", stderr)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -55,9 +55,9 @@ func CompareOsEnv(key, comparedEnv string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ExecCommand execute command, return the stdout and stderr string of command, and error if error occur
|
// ExecCommand execute command, return the stdout and stderr string of command, and error if error occur
|
||||||
// param `command` is a complete command strinig, like, ls -a (linux), dir(windows), ping 127.0.0.1
|
// param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1
|
||||||
// for linux, use /bin/bash -c to execute command
|
// in linux, use /bin/bash -c to execute command
|
||||||
// for windows, use powershell.exe to execute command
|
// in windows, use powershell.exe to execute command
|
||||||
func ExecCommand(command string) (stdout, stderr string, err error) {
|
func ExecCommand(command string) (stdout, stderr string, err error) {
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
var errOut bytes.Buffer
|
var errOut bytes.Buffer
|
||||||
|
|||||||
Reference in New Issue
Block a user