0

I am trying to get the following script to execute properly via PHP exec. It works fine in the Linux terminal (returning the average CPU core temperature):

sensors | perl -ne 'if (/^Core \d+:\s+\+(.*?)°C/) { $s += $1; $c++; } END { printf("%.1fC", $s/$c) }'

I have tried to escape the double quotes, but realize there may be another issue. I am using the script in PHP like this:

exec("sensors | perl -ne 'if (/^Core \d+:\s+\+(.*?)°C/) { $s += $1; $c++; } END { printf("%.1fC", $s/$c) }'", $results);
echo $results;

EDIT: escapeshellcmd is probably the right direction, but I am having trouble seeing how to implement it.
user1218172
  • 193
  • 1
  • 12
  • Are `$` and `\ ` special in PHP double-quoted string literals? If so, you will need to escape them – ikegami Mar 15 '22 at 04:03
  • @Andrea Olivato, I don't see how? – ikegami Mar 15 '22 at 04:05
  • Have you read the answer? Did you try `escapeshellcmd`? [Official Docs](https://www.php.net/manual/en/function.escapeshellcmd.php) – Andrea Olivato Mar 15 '22 at 04:06
  • I don't know enough about PHP, but to see that the correct command is passed to the shell, I would store the whole string into a variable, and print the variable, before passing it to `exec`. – user1934428 Mar 15 '22 at 07:17

0 Answers0