我想获取命令 php -r 'echo get_cfg_var("some_var");'
的输出根据预定义值检查它。目前,我有以下代码:
variableName := "default_mimetype"
cmd := exec.Command("php", "-r", "'echo get_cfg_var(\"" + variableName + "\");'")
out, err := cmd.CombinedOutput()
运行后,
err.Error()
返回"exit status 254"
out
是 "PHP Parse error: syntax error, unexpected end of file in Command line code on line 1"
是什么导致了这个错误?我是否没有正确地逃避某些事情?
最佳答案
问题出在你的论点上。如果您更改写入 shell 命令的内容,它将如下所示:
$ php -r "'echo get_cfg_var(\"default_mimetype\");'"
您会注意到第二个参数周围有一组额外的引号导致语法错误。您可以通过将 exec.Command
更改为以下内容来解决此问题:
cmd := exec.Command("php", "-r", "echo get_cfg_var(\"" + variableName + "\");")
关于php - 在 Go 中获取 PHP 配置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30875893/