1

i am trying to execute unix command in php script like this.

<?php

echo shell_exec('head -n 1 log_list_23072014|awk  -F ',' '{print $2}'');

?>

This is the file , trying to get the first column of the first row.

NODE,CGR,TERMID,VMGW,ET

but the error message i am getting

Parse error: syntax error, unexpected 'shell_exec' (T_STRING), expecting ',' or ';'.

cant able to find please help.

3
  • shell_exec() expects 1 parameter, you're parsing 2. Commented Jul 24, 2014 at 5:31
  • epects 1 parameter means, i dint get and the parameters which i passed is correct or wrong or else please share the code to get 1st column of the 1st row Commented Jul 24, 2014 at 5:33
  • You're doing this: shell_exec(FIRSTITEM , SECOND ITEM); and the function can only do this shell_exec(FIRSTITEM); Commented Jul 24, 2014 at 5:35

2 Answers 2

1

The string you've used is not valid, you have to escape single quotes inside your string:

<?php echo shell_exec('head -n 1 log_list_23072014|awk  -F \',\' \'{print $2}\'');

You can also use exec()

Sign up to request clarification or add additional context in comments.

1 Comment

Slight tweak: you need not quote the comma, and then you need not escape those quotes. In addition, awk can do the equivalent of head -n1 by itself: ' awk -F, \'NR==1{print $2;exit}\' '
1

There was an extra ' within your command. Using your command in a variable can help with identifying errors, and when using the standard exec it's required.

$cmd = 'head -n 1 log_list_23072014 | awk -F , \'{print $2}\'';
echo shell_exec($cmd);

changing it to the format above should work.

1 Comment

Wrong. read the manual. Only one $cmd can be passed. EDIT: +1 better for referencing exec

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.