duanliang1999 2015-06-16 18:53
浏览 17
已采纳

在Go中获取PHP配置变量

I want to get the output of the command php -r 'echo get_cfg_var("some_var");' to check it against a predefined value. Currently, I have the following code:

variableName := "default_mimetype"
cmd := exec.Command("php", "-r", "'echo get_cfg_var(\"" + variableName + "\");'")
out, err := cmd.CombinedOutput()

after running,

err.Error() returns "exit status 254"

out is "PHP Parse error: syntax error, unexpected end of file in Command line code on line 1"

What is causing this error? Am I not escaping something properly?

  • 写回答

1条回答 默认 最新

  • dthtvk3666 2015-06-16 19:25
    关注

    The problem is your argument. If you change what you have written into a shell command, it would look like the following:

    $ php -r "'echo get_cfg_var(\"default_mimetype\");'"
    

    You will notice that there is an extra set of quotes around the 2nd argument that is causing the syntax error. You can fix this by changing your exec.Command to the following:

    cmd := exec.Command("php", "-r", "echo get_cfg_var(\"" + variableName + "\");")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法