我需要使用ipmitool获取dimm信息,如下所示:
exec("/usr/bin/ipmitool -I lan -H $spip -U root -P '$thepassword' sunoem cli 'show System/Memory/DIMMs/$a' | grep -i location", $dimm_loc, $ipmiretval);
$ a之前定义为: $ a = $ DIMM [$ i]于
上面exec命令的返回值是1.如果我用它的vaule替换$ a,即
exec("/usr/bin/ipmitool -I lan -H $spip -U root -P '$thepassword' sunoem cli 'show System/Memory/DIMMs/D5' | grep -i location", $dimm_loc, $ipmiretval);
exec命令按预期执行。所以看起来$ a不能用在上面的例子中。我怎样才能将变量传递给exec命令?
谢谢!
答案 0 :(得分:2)
运行var_dump($a)
并查看它实际包含的内容。同时将执行的命令转储为字符串,以查看命令格式是否正确。
答案 1 :(得分:1)
还有一个提示。而不是执行exec("some very long command line")
,而是将命令行放在变量中,然后将LOG和exec()放在变量中。例如:
$fmt="/usr/bin/ipmitool -I lan -H %s -U root -P '%s' sunoem cli 'show System/Memory/DIMMs/%s' | grep -i location";
$cmd=sprintf($fmt, $spip, $thepassword, $a);
exec($cmd, $dimm_loc, $ipmiretval);
syslog(LOG_DEBUG, "Running: $cmd");
if ($ipmiretval > 0) {
syslog(LOG_ERR, "exec FAILED: $cmd");
} else {
syslog(LOG_DEBUG, "exec: $cmd");
}