Golang exec.Command的输出为空(没有错误)。

huangapple go评论80阅读模式
英文:

Golang exec.Command Output blank(no error)

问题

我正在尝试在Go中运行一个sqlite3命令,进行一个简单的查询(我完全理解从Go中访问sqlite还有其他方法,但这不是问题所在)。

func sqliteTest(){
    cmd := exec.Command("/usr/bin/sqlite3", "/home/chadg/Downloads/testDb.db", "'select * from testTable;'")
    fmt.Println("Command String:", cmd.String())
    out, err := cmd.CombinedOutput()
    if err != nil {
        fmt.Println("Error Accessing Database:", err.Error(), string(out))
        return
    }
    fmt.Println("Result Length:", len(out))
    fmt.Println("Result:", string(out))
}

运行这段代码会得到以下输出:

Command String: /usr/bin/sqlite3 /home/chadg/Downloads/testDb.db 'select * from testTable;'
Result Length: 0
Result: 

Process finished with exit code 0

然而,如果我手动运行相同的命令:

$ /usr/bin/sqlite3 /home/chadg/Downloads/testDb.db 'select * from testTable;'
testName|testValue

在Go中运行命令时,我确实遇到了许多问题,但通常会生成错误,但这次既没有错误,stdout和stderr的结果似乎都为空。

编辑:
单引号和双引号没有改变任何结果。

cmd := exec.Command("/usr/bin/sqlite3", "/home/chadg/Downloads/testDb.db", "\"select * from testTable;\"")

仍然返回空输出,但从命令行中按预期工作。

英文:

I am attempting to run a sqlite3 command from within Go, to do a simple query (I completly understand there are other ways to access sqlite from go, but thats not the issue here)

func sqliteTest(){
	cmd:=exec.Command("/usr/bin/sqlite3", " /home/chadg/Downloads/testDb.db 'select * from testTable;'")
	fmt.Println("Command String:",cmd.String())
	out,err:=cmd.CombinedOutput()
	if err!=nil{
		fmt.Println("Error Accessing Database:",err.Error(),string(out))
		return
	}
	fmt.Println("Result Length:",len(out))
	fmt.Println("Result:",string(out))

}

Running this gives me this output:

Command String: /usr/bin/sqlite3  /home/chadg/Downloads/testDb.db 'select * from testTable;'
Result Length: 0
Result: 

Process finished with exit code 0

However if I run the same command by hand:

$ /usr/bin/sqlite3  /home/chadg/Downloads/testDb.db 'select * from testTable;'
testName|testValue

Have certainly run into many issues when running commands from within go, but there is always an error generated, but this time, there is no error, but the result from stdout and stderr seems to both be empty.

EDIT:
Single quotes vs double quotes doesn't change anything

cmd:=exec.Command("/usr/bin/sqlite3", " /home/chadg/Downloads/testDb.db \"select * from testTable;\"")

still returns an empty output, but works as expected from the command line.

答案1

得分: 2

exec.Command中的参数应该作为单独的字符串传递。

cmd := exec.Command("/usr/bin/sqlite3", "/home/chadg/Downloads/testDb.db", "select * from testTable;")

至于你观察到的行为:

当以非交互方式启动sqlite3并传入一个参数时,它将把该参数作为数据库文件名,并从stdin读取查询语句,但是cmd.CombinedOutput()会关闭stdin,导致输出为空。

英文:

Arguments in exec.Command should be passed as separate strings.

cmd := exec.Command("/usr/bin/sqlite3", "/home/chadg/Downloads/testDb.db", "select * from testTable;")

As for the behavior you're observing:

When sqlite3 is started non-interactively with 1 argument, it treats that as the database file name and reads the query from stdin, but cmd.CombinedOutput() closes stdin, resulting in an empty output.

huangapple
  • 本文由 发表于 2021年8月27日 04:48:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/68945140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定