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.
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()
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.
Related
I am using the below code to get input from user and modify my backup.php file:
#!/bin/bash
read -p "Enter hostname: " hostname
read -p "Enter cPanel username: " user
read -p "Enter password: " pass
echo "\$source_server_ip = \"$hostname\";" >> "backup.php"
echo "\$cpanel_account = \"$user\"; " >> "backup.php"
echo "\$cpanel_password = \"$pass\"; " >> "backup.php"
This works perfectly; however, I want to insert the user-provided details in backup.php at line numbers 4, 5 and 6, respectively.
backup.php contents:
Line no. 1: php
Line no. 2: include xmlapi.php
Line no. 3: (blank)
Line no. 4: $source_server_ip = " ";
Line no. 5: $cpanel_account = " ";
Line no. 6: $cpanel_password = " ";
Line no 7 L code continue..
I want to keep reset of the code as it is and want to make changes in line no 4,5.6 online.
How can I do this? Do I need to use sed?
If I understand correctly, you need to replace/update your 4th, 5th and 6th line with some new lines. You can use sed's substitution command for this:
host_line="\$source_server_ip = \"$hostname\""
user_line="\$cpanel_account = \"$user\""
pass_line="\$cpanel_password = \"$pass\""
sed -i "4s/.*/$host_line/; 5s/.*/$user_line/; 6s/.*/$pass_line/" backup.php
#different notation:
sed -i -e "4s/.*/$host_line/" -e "5s/.*/$user_line/" -e "6s/.*/$pass_line/" backup.php
4s determines on which line the substitution command should take place (in this case 4th line)
.* matches the entire line and substitutes it with your user input
-i is in-place editing. It will edit your file instead of sending the result to stdin
Warning: using sed's s command is really straightforward but it can have some surprising and dangerous results if your variables contain unescaped special characters to sed, for example: &, /, newline, ...
Or make use of the sed's c command:
sed -i "4c \
$host_line
5c \
$user_line
6c \
$pass_line
" backup.php
#different notatiton:
sed -i -e "4c $host_line" -e "5c $user_line" -e "6c $pass_line" backup.php
Warning: you can manage to break this as well, with unescaped newline for example:
pass='mypass
2c oops'
You will either have to escape those special characters to sed or use what I consider the safest solution, awk:
awk -i inplace -v hl="$host_line" -v ul="$user_line" -v pl="$pass_line" '
NR==4 { $0=hl }
NR==5 { $0=ul }
NR==6 { $0=pl }
{ print }' backup.php
Do not forget to use -r option with read or it will treat backslashes specially.
Also, note that even backslashes in your variables will later get interpreted! You can add extra backslashes with parameter expansion if you want to prevent this:
host_line="\$source_server_ip = \"${hostname//\\/\\\\}\""
user_line="\$cpanel_account = \"${user//\\/\\\\}\""
pass_line="\$cpanel_password = \"${pass//\\/\\\\}\""
EDIT: If you just want to insert the 3 lines after the 3th line, use this simple sed:
sed -i "4i $host_line\n$user_line\n$pass_line" backup.php
when i try parsing a file by executing this command from within php using shell_exec():
$shellCommand = "cat $filelocn | awk 'BEGIN{RS="<br>"}{$1=$1}1' |sed '/CURRENT/d' ";
echo $shellCommand ;
An error is displayed:
PHP Parse error: syntax error, unexpected '>' in filename.php
i also tried adding \ before ' ie: "cat $filelocn | awk \'BEGIN{RS=\"<br>\"}{$1=$1}1'";
but it again throws error.
How do i resolve this issue ?
Unless you're trying to interpolate variables in your PHP code into your string, you need to escape the dollar signs too:
$shellCommand = "cat \$filelocn | awk 'BEGIN{RS=\"<br>\"}{\$1=\$1}1' |sed '/CURRENT/d' ";
echo $shellCommand;
Specifically, the $1s are causing your parse error. If $filelocn is a PHP variable, you don't need to escape it.
I have a semi-lengthy shell command that I need to execute from within php. I feel like I have everything properly escaped, however it is not working properly when executed from php's exec() function. When I run the command with exec() it exits without error, but does not run properly.
I am comparing a list file that has email addresses with additional pipe delimited information against a text file containing the md5 version of email addresses to suppress from the original list.
Here is the bash command:
while read line ; do email=`echo "$line" | cut -d '|' -f1` ; if [ $(E=`echo -en "$email" | md5sum | awk '{print $1}'`; grep $E /path/to/suppressions | head -1 ;) ] ; then continue ; else echo $line ; fi done < /path/to/emails
/path/to/suppressions is a text file, containing the md5 version of an email address to suppress (it contains the md5 for "emailtosuppress#domain.com":
edb5feb3be7d0a4e1e250ccdf0c04ace
/path/to/emails is a text file containing a list of email addresses with some other delimeted information:
emailtokeep#domain.com|1000|1
emailtosuppress#domain.com|1000|1
When the command is executed in bash, the output is simply the email address that does NOT exist in the suppression list, and it works flawless:
emailtokeep#domain.com|1000|1
The trouble I am having is when I execute this same command with exec() in php, it outputs all the lines from the "emails" file, without suppressing any matches from the suppression file.
$supCommand = 'while read line ; do email=`echo "$line" | cut -d \'|\' -f1` ; if [ $(E=`echo -en "$email" | md5sum | awk \'{print $1}\'`; grep $E /path/to/suppressions | head -1 ;) ] ; then continue ; else echo $line ; fi done < /path/to/emails' ;
exec($supCommand, $supStatus) ;
print_r($supStatus) ;
I enclosed the command in single quotes, and escaped the four instances of single quotes within the command. If anyone has any ideas I would be extremely GRATEFUL.
Thanks in advance :)
This seems to work:
<?php
$bash = '
mapfile -t md5sums < suppressions
function md5 { printf "%s" "$1" | md5sum | cut -f1 -d" "; }
while IFS="|" read -r email b c; do
this_md5=$(md5 "$email")
for suppress in "${md5sums[#]}"; do
[[ $this_md5 == $suppress ]] && continue 2
done
echo "$email|$b|$c"
done < emails
';
$cmd = "bash -c '$bash'";
$keep = passthru($cmd);
print($keep);
?>
Good Day All
I'm having a problem getting this command to work on button press
I need to search through a file and output the results in comma delimited text.
PHP doesnt seem to like the curly brackets so as far as I could read I need to use exec(), but to no avail as the error I recieve is "syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$'"
I'm still new to php so sorry if the answer is obvious!
Any info will be helpful, Thanks
<html>
<body>
<h1>Linux Command Test</h1>
<form method="POST" action="">
<input type="submit" id="submit" name="submit" value="Submit"/>
</form>
<?php
if (isset($_POST['submit']))
{
$output = exec('grep -i hello test.txt | awk -v OFS=, '{\$1=\$1;print}' > newtest.txt');
echo "<pre>$output</pre>";
}
?>
</body>
</html>
try escaping the $
grep -i hello test.txt | awk -v OFS=, '{\$1=\$1;print}' > newtest.txt
$output = exec('grep -i hello test.txt | awk -v OFS=, '{\$1=\$1;print}' > newtest.txt');
should be:
$output = exec('awk -v OFS=, \'/hello/i {$1=$1;print}\' test.txt > newtest.txt');
You need to escape the quotes, otherwise they delimit the PHP string. You don't need to escape $ inside single-quoted strings in PHP. You shouldn't redirect the awk output to a file if you want to capture it in a PHP variable.
And there's no need to pipe grep to awk, since awk has built-in pattern matching.
There are bunch of functions you can use in PHP to invoke external applications, exec(), system() etc: http://www.php.net/manual/en/ref.exec.php but mind that it's up to administrator to let you use them. On most shared hostings these functions are disabled
I would like to execute a shell command through php and display it in a browser. Is there anyway to do so?
here is my php code : [test.php]
<?php
$number=$_GET["num"];
$date=$_GET["date"];
$output = shell_exec('egrep -w '2012-09-01|974' /home/myquery_test/log/push.log');
echo "<pre>$output</pre>";
?>
When I run this(test.php) file from browser nothing shows up. But when i change the
$output = shell_exec('ls')
its working fine!! Why isn't the egrep/grep command not working??
The egrep command isn't working, because you're using single quotes as a string constant delimiter: 'egreep -w' <==> 2012-09-01|974' <==> /home/myquery_test/log/push.log' <==Just use double quotes in the string, or as string delimiters OR escape the quotes.
shell_exec('egrep -w \'2012-09-01|974\' /home/myquery_test/log/push.log');
shell_exec('egrep -w "2012-09-01|974" /home/myquery_test/log/push.log');
shell_exec("egrep -w '2012-09-01|974' /home/myquery_test/log/push.log");
And, to avoid not getting the warnings and errors that would have brought this issue to light when testing, set your ini to E_STRICT|E_ALL, and fix the warnings, rather then ignoring them. [teasingly: after you're done with that, you might want to consider accepting some answers]I see you've accepted a lot while I was typing this post up :)
Using variables in your command:
$output = shell_exec("tail -f | egrep '$number.*$date' /var/www/myquery_test/log/push.log");
$output = shell_exec('tail -f | egrep "'.$number.'.*'.$date.'" /var/www/myquery_test/log/push.log');
$output = shell_exec("tail -f | egrep \"$number.*$date\" /var/www/myquery_test/log/push.log");