Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d94beb6

Browse files
committed
handle shell stdout
1 parent 2575719 commit d94beb6

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

‎demo/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
本目录会针对场景不定时添加一些`go`的代码片段
2+
3+
- handle_stdout -- 如何调用脚本并处理结果

‎demo/handle_stdout/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- 调用脚本并处理Stdout和Stderr
2+
- 可变参数(slice数据接口则参照注释掉的两行,分别是定义与传参)

‎demo/handle_stdout/echo.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#测试 shift 命令(x_shift.sh)
2+
until [ $# -eq 0 ]
3+
do
4+
echo "第一个参数为: 1ドル 参数个数为: $#"
5+
shift
6+
done

‎demo/handle_stdout/main.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os/exec"
7+
)
8+
9+
// func runSomeShell(args ...string) {
10+
func runSomeShell(args []string) {
11+
cmd := exec.Command("sh", args...)
12+
var out, err bytes.Buffer
13+
cmd.Stdout = &out
14+
cmd.Stderr = &err
15+
error := cmd.Run()
16+
if error != nil {
17+
fmt.Println("Run failed:\n", err.String())
18+
return
19+
}
20+
fmt.Printf(out.String())
21+
}
22+
23+
func main() {
24+
// runSomeShell("./echo.sh", "1", "2", "3")
25+
runSomeShell([]string{"./echo.sh", "1", "2", "3"})
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /