英文:
How can I turn bash exec.Command returns into Arrays?
问题
我正在尝试找到以hello.go结尾的文件。
statement := "find "CurrentDirectory" -print | grep -i hello.go"
result, error := exec.Command("bash", "-c", statement).Output()
这给我一个包含2个文件路径的列表,我尝试将它们转换为我可以单独访问的数组:
Directory_array := strings.Split(string(result),"\n")
fmt.Println(len(Directory_array))
数组的长度显示为"3",但除了第0个位置外,数组是空的。
这些代码行独立工作,但是一起使用时不起作用。如何使数组填充为单独的路径?
英文:
I'm trying to find files that end in hello.go
statement:= "find "CurrentDirectory" -print | grep -i hello.go"
result, error := exec.Command("bash", "-c", statement).Output()
This gives me a list containing 2 file paths and I try to turn them into arrays that I can individually address using:
Directory_array := strings.Split(string(result),"\n")
fmt.Println(len(Directory_array) )
The length of the array shows as "3" but the array is empty except for the 0th position.
Independently the code lines work but not together. How can I get the array to fill with individual paths?
答案1
得分: 2
在你的情况下,使用strings.FieldsFunc而不是strings.Split。例如:
statement := "find . -print | grep -i hello"
result, err := exec.Command("bash", "-c", statement).Output()
if err != nil {
    panic(err)
}
ds := strings.FieldsFunc(string(result), func(r rune) bool {
    return r == '\n'
})
for i, d := range ds {
    fmt.Println(i, d)
}
进一步阅读:
英文:
In your case, use strings.FieldsFunc instead of strings.Split. For example:
statement:= "find . -print | grep -i hello"
result, err := exec.Command("bash", "-c", statement).Output()
if err != nil {
	panic(err)
}
ds := strings.FieldsFunc(string(result), func(r rune) bool {
	return r == '\n'
})
for i, d := range ds {
	fmt.Println(i, d)
}
Further reading:
答案2
得分: 0
你可以使用stdout管道,并使用Scanner逐行读取。
func main() {
    cmd := exec.Command("ls", "-l")
    out, _ := cmd.StdoutPipe()
    s := bufio.NewScanner(out)
    cmd.Start()
    defer cmd.Wait()
    for s.Scan() {
        fmt.Println("new line:")
        fmt.Println(s.Text())
    }
}
如果你想将其存储到一个切片中,你可以事先创建切片并将其追加到其中。
lines := make([]string, 0, 10)
for s.Scan() { lines = append(lines, s.Text()) }
英文:
You could use the stdout pipe and read line by line using a Scanner.
func main() {
	cmd := exec.Command("ls", "-l")
	out, _ := cmd.StdoutPipe()
	s := bufio.NewScanner(out)
	cmd.Start()
	defer cmd.Wait()
	for s.Scan() {
		fmt.Println("new line:")
		fmt.Println(s.Text())
	}
}
If you want to store this into a slice, you can create the slice beforehand and append to it.
lines := make([]string, 0, 10)
for s.Scan() { lines = append(lines, s.Text()) }
答案3
得分: -1
你的代码中漏掉了调用Run()函数。以下是修复方法:
statement := "find \"CurrentDirectory\" -print | grep -i hello.go"
cmd := exec.Command("bash", "-c", statement)
err := cmd.Run()
if err != nil {
    // 处理错误
}
result, error := cmd.Output()
另外,为什么不使用os.Walk()呢?
英文:
You have missed calling Run() in your code. Here is how to fix it:
statement:= "find "CurrentDirectory" -print | grep -i hello.go"
cmd := exec.Command("bash", "-c", statement)
err := cmd.Run()
if err != nil {
    ...
}
result, error := cmd.Output()
P.S. Why not to use os.Walk()?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论