PHP exec with JQ -[]括号错误:PHP致命错误:不能使用[]进行读取


PHP exec with JQ - [] brackets error: PHP Fatal error: Cannot use [] for reading

在PHP脚本中有以下命令:

shell_exec('cd /home/scripts/scripts; ./total.sh | jq '.hits .hits [] .fields["termListData.terms"] | .[]' | wc -l > /home/data/total.csv');
PHP错误:Cannot use [] for reading…

命令单独从命令行工作完美,但不是在PHP脚本中。我做错了什么?

问题是你在命令执行中使用了单引号

你可以把你的命令分成两个

$jq_args = '.hits .hits [] .fields["termListData.terms"] | .[]';
$cmd = 'cd /home/scripts/scripts; ./total.sh | jq '.escapeshellarg($jq_args).' | wc -l > /home/data/total.csv';
shell_exec($cmd);