Hi i have a Linux command what uses a pipe to obtain its result. It basically search a pdf for a string and returns the page numbers of all of the occurrences.
pdftotext /var/www/html/custom/test.pdf - | awk -vRS=$'\\f' -vNAME='06/1133/13' \ 'index($0,NAME){printf \"%d,\", NR}'
The result of this on the command line looks like this:
8,9,10,11,
Trying to run this in php with this code
$command = "pdftotext /var/www/html/custom/test.pdf - | awk -vRS=$'\\f' -vNAME='06/1133/13' \ 'index($0,NAME){printf \"%d,\", NR}'";
echo shell_exec($command);
i get the following output
1,
I have used the php script to print out the command variable to make sure the formatting is correct. I can run the output command on the terminal and this works with out issue.
What is needed within the PHP script to allow this to run?
Related
Prokka is a tool used to annotate bacterial genomes and can be installed and access using the command line in the local system or server.
So, I have downloaded Prokka using Git clone (git clone https://github.com/tseemann/prokka.git) and complied it on the CentOS (7) server along with all the dependencies with the latest version (NCBI-BLAST 2.10.1, hmmer 3.3, gnu Parallel).
In order to make a prediction server, I have created a webpage using PHP, and in that PHP file, I'm taking input from the user and passing that input to a shell file, and trying to execute that shell file using the shell_exec() function.
Here $fileName and $fn are the input from taken from the webpage.
$output = shell_exec("./path/to/shell/file/invoke.sh $script_path/$fileName $fn");
echo "<pre>$output</pre>";
invoke.sh file:
input_genome="$1";
prefix_genome="$2";
/path/to/prokka/bin/prokka $input_genome --outdir ./results/"$prefix_genome" --prefix $prefix_genome --locustag $prefix_genome --cpus 8 --kingdom Bacteria
Now, if I'm invoking Prokka (/path/to/prokka/bin/prokka) directly from the command line it's working fine and create all the files which ideally it should create. Moreover, if I'm running the shell script (invoke.sh) to invoke the Prokka (/path/to/prokka/bin/prokka) it's again working fine and generate all the files.
Expected and actual output files:
seq.faa
seq.err
seq.fna
seq.txt
seq.ffn
seq.gff
seq.tsv
seq.fsa
seq.gbk
seq.tbl
seq.log
seq.sqn
But the problem is while I'm running the PHP file from the web-browser as mentioned above to run the shell file (invoke.sh) it is running, but the Prokka command is not running properly inside the shell file and therefore not generating all of the output files.
I checked the error_log it showed me this:
[23:43:57] Will use blast to search against /home/group01/html/csspred/prokka/db/kingdom/Bacteria/sprot with 8 CPUs
[23:43:57] Running: cat \/home\/group01\/html\/csspred\/results\/01\-Nov\-23_43_779958414\/seq\/seq\.sprot\.tmp\.18401\.faa | parallel --gnu --plain -j 8 --block 3279 --recstart '>' --pipe blastp -query - -db /home/group01/html/csspred/prokka/db/kingdom/Bacteria/sprot -evalue 1e-09 -qcov_hsp_perc 80 -num_threads 1 -num_descriptions 1 -num_alignments 1 -seg no > \/home\/group01\/html\/csspred\/results\/01\-Nov\-23_43_779958414\/seq\/seq\.sprot\.tmp\.18401\.blast 2> /dev/null
[23:44:00] Could not run command: cat \/home\/group01\/html\/csspred\/results\/01\-Nov\-23_43_779958414\/seq\/seq\.sprot\.tmp\.18401\.faa | parallel --gnu --plain -j 8 --block 3279 --recstart '>' --pipe blastp -query - -db /home/group01/html/csspred/prokka/db/kingdom/Bacteria/sprot -evalue 1e-09 -qcov_hsp_perc 80 -num_threads 1 -num_descriptions 1 -num_alignments 1 -seg no > \/home\/group01\/html\/csspred\/results\/01\-Nov\-23_43_779958414\/seq\/seq\.sprot\.tmp\.18401\.blast 2> /dev/null
and generated only four files :
seq.HAMAP.hmm.tmp.12617.faa
seq.HAMAP.hmm.tmp.12617.hmmer3
seq.fna
seq.log
I even used a C file to invoke the execute a shell file which ultimately invokes the Prokka command but again the problem remained the same.
PHP: $output = shell_exec("./invoke.out $script_path/$fileName $fn");
C:
`int main(int argc, char *argv[])
{
char command[1000];
sprintf(command, "%s %s %s", "sh /home/group01/html/csspred/scripts/CSS_pred_new_additions/prokka_test.sh", argv[1], argv[2]);
system(command);
}`
What is the problem and how to solve this? I just wanted to execute my invoke.sh or invoke.out so that at the backend my Prokka command will be able to run correctly and gives me all expected output files.
I have installed Sage math on my ubuntu server. I can run sage commands by ssh terminal such as
$sage:
$sage: f = x^2
$sage: f.diff(x)
I would like to do on a php script
exec('sage');
exec('sage: f = 5x^3');
$fprime = exec('sage: latex(f.diff(x))');
echo $fprime;
I would expect "15x^2" as output but that's not happening .. however on ssh terminal all is good..
all help would be greatly appreciated..
I'm not completely sure of what you want to achieve. PHP's exec function executes a shell command, and that's all. Read http://fr2.php.net/manual/en/function.exec.php.
sage: f = 5*x^3 is not a shell command. You can run sage scripts directly via the command line using the -c switch :
./sage -c 'print latex((5*x^3).diff())'
will print 15 \, x^{2} to the terminal. In PHP you can grab this output by passing a second argument to exec:
exec("./sage -c 'print latex((5*x^3).diff())'", $output);
echo $output[0];
I have a PHP file with shell commands running through a screen, the commands run fine but I was wondering if there was a way to get this to output to PHP without writing to another file and reading it on PHP's end.
PHP - trace() is just a fancy print_r()
$cmd = 'ls -h /';
trace(shell_exec('screen -S output -p 0 -X stuff "`echo '.$cmd.'\'\r\n\'`"'));
Web output
NULL
Screen output
www-data#:/home/ubuntu$ ls -h /
bin build etc initrd.img lib media opt root selinux sys usr vmlinuz
boot dev home initrd.img.old lost+found mnt proc sbin srv tmp var vmlinuz.old
Any suggestions?
--Edit--
Certain commands aren't outputting directly, one of the reasons I'm using screen
PHP
$cmd = 's3ls';
trace(shell_exec('screen -S output -p 0 -X stuff "`echo '.$cmd.'\'\r\n\'`"'));
trace(shell_exec($cmd));
Web output
trace:NULL
trace:NULL
Screen
www-data#:/home/ubuntu$ s3ls
+---------------+--------------------------+
| Name | CreationDate |
+---------------+--------------------------+
| bucket | 2012-05-31T13:08:51.000Z |
| bucket | 2012-01-17T16:51:58.000Z |
| bucket | 2012-03-31T11:19:54.000Z |
+---------------+--------------------------+
void passthru ( string $command [, int &$return_var ] )
The passthru() function is similar to the exec() function in that it
executes a command. This function should be used in place of exec() or
system() when the output from the Unix command is binary data which
needs to be passed directly back to the browser. A common use for this
is to execute something like the pbmplus utilities that can output an
image stream directly. By setting the Content-type to image/gif and
then calling a pbmplus program to output a gif, you can create PHP
scripts that output images directly.
Though apparently it may be a bit flaky.
I'm working on interfacing a microcontroller with a lamp server. I am trying to run the command echo -e -n "data \r" > /dev/ttyUSB0 using shell_exec in php but with no results. It works just fine from the command line. Doing a little experimenting, I discovered that echo -e -n "1 \r" actually echoes -e -n 1. Is there a reason it won't take the -e or -n options?
Here's my code:
<?php
shell_exec('echo -e -n "1 \r" > /dev/ttyUSB0');
?>
Instead of using shell_exec and echo, why not use PHP's filesystem functions?
file_put_contents('/dev/ttyUSB0', "1 \r");
There are some other functions too, try this function maybe you get your answer.
exec(command, $output);
This function takes a command and assigns to $output an array where each element is a line of the generated output.
I ran into similar problem, calling from php
php > echo shell_exec("echo -e aaa\tbbb");
-e aaa bbb
note, that output contains "-e", while I have expected that 'echo' command will interpret -e as flag and would not send it to output.
After doing some investigation I came to following conclusion:
when exec or shell_exec are called from php - new shell interpreter is launched.
php launches "sh".
When I was running on CentOS sh was a symlink to bash, and exec("echo ...") worked as I would have expected.
Now I am running Ubuntu. sh is a symlink to dash, not bash!
And final root cause - builtin echo command in dash does not have/understand '-e' flag, so it just forwards it to output
This is an old question but it goes what worked for me in case someone else comes across with this as well.
Yesterday night I've messing around and struggling with this myself. For this to work, the command you need to use should start by calling the echo bin directly /bin/echo... instead of only echo.
Also don't forget to use single quote /bin/echo... instead of double quote to avoid PHP null byte detection error (this part you did correctly).
I had a similar problem running with -e, I was trying to change the password from a php
running this didnt work
exec("echo -e \"$pass\\n$pass\" | passwd $user");
It said passwords dont match. Checking the echo i saw that -e was includes as a part of the echoing so i changed it to
exec("echo \"$pass\\n$pass\" | passwd $user");
And then it worked.
Trying to run the following command in php to run powershell command...
the following works:
$output = shell_exec(escapeshellcmd('powershell get-service | group-object'));
I can not run it like this:
$output = shell_exec('powershell get-service | group-object');
it will not pass the pipe | character
but if I try to run:
$output = shell_exec(escapeshellcmd('powershell get-service | where-object {$_.status -eq "Running"}'));
I get no output.
The following:
$cmd = escapeshellcmd('powershell get-service | where-object {$_.status -eq "Running"}');
returns:
powershell get-service ^| where-object ^{^$_.status -eq ^"Running^"^}
Any suggestions on why this is happening and how to fix this?
Edit: Also I could run it as .ps1 script but I want to be able to pass $var to it.
I'll take a stab although I have no PHP experience whatsoever.
I have a feeling that what's happening is your pipe character is being interpreted by the command shell instead of PowerShell. For example if you ran the following at the cmd.exe command prompt:
dir /s | more
The output of the first command gets piped to the input of the second just like you'd expect in PowerShell.
Escaping the string will only make the problem worse because you're transforming the string in such a way that PowerShell has no idea how to unescape it.
Try enclosing your original PowerShell expression in a quote like the following:
$output = shell_exec('powershell.exe -c "get-service | group-object"');
Or preferably, it looks like there's an exec() function that does not go through the command shell. This might work better.
$output = exec('powershell.exe -c get-service | group-object');
'powershell get-service | group-object'
will be interpreted as
run powershell and pass it get-service as an argument
then pipe the output of powershell to group_object (i.e. not the output of get-service)
What you want is for powershell to see get-service | group-object as it's argument, so you have to enclose that in quotes, like this.
$output = shell_exec('powershell "get-service | group-object"');