在php中运行系统命令

I want to run system command in PHP.But when I run file I am getting error. My Command like.I want to run awk command.

Code:

  $awk="awk '{print $1,$NF}' stat.txt";
  $awkop=shell_exec($awk);

Output:

awk: {print $1,} 
awk: ^ syntax error awk: cmd. line:1: {print $1,} 
awk: cmd. line:1: ^ unexpected newline or end of string 

You are making use of incorrect escape and quotes due which your shell commands are passed in incorrect way to the shell.

You must rewrite it as following:

$awk='awk \'{print $1,$NF}\' stat.txt';
$awkop=shell_exec($awk);

This will work.

begining of your code, there is a ` sign, please remove it first. And also make sure, you have a stat.txt file in your file execution location and writable to execution user.

$awk="awk '{print $1,$NF}' stat.txt";
$awkop=shell_exec($awk);

I tried this, no errors. Also no any output to the file ....