PHP system() or shell_exec(): execute another shell - php

I'm using system() and shell_exec() but it seems that this funcions use /bin/sh always. I want to use another shell when I call this functions: bash or zsh. Is it possible?

What's the point? If you use system to start another shell script, that shell script itself can decide (via its #! line) which shell it is supposed to be processed by. Aside from this, you can always explicitly call the shell. For instance, if you want to take advantage of globbing operators specific to zsh or specific zsh builtins, you can do a
# Produce list of files including hidden files. Use
# the zsh-builtin command instead of /usr/bin/echo :
system('zsh -c "echo *(DN)"')

One solution can be:
<?php
\system('bash -c "here your command"');
or
<?php
\system('zsh -c "here your command"');

Related

Run cmd in php with > operator

I want to run a simple shell command, say:
dir > bau.txt
Using php:
$cmd = escapeshellcmd ('dir > bau.txt');
shell_exec($cmd);
But it does not work (bau.txt stays empty).
Is there any reason for that?
A normal $cmd would work (say just 'dir').
This seems like a similar scenario:
How to run multiple commands in system, exec or shell_exec?
Essentially when you use the > operator, you're piping your standard output to the file you've specified. It seems that shell_exec doesn't directly support piping, but rather, the output needs to be stored from shell_exec and then run through shell_exec with the next command.

I need to run a python script from php

I have to design a interface using PHP for a software written in python. Currently this software is used from command line by passing input, mostly the input is a text file. There are series of steps and for every step a python script is called. Every step takes a text file as input and an generates an output text file in the folder decided by the user. I am using system() of php but I can't see the output but when I use the same command from command line it generates the output. Example of command :
python /software/qiime-1.4.0-release/bin/check_id_map.py -m /home/qiime/sample/Fasting_Map.txt -o /home/qiime/sample/mapping_output -v
try this
$script = 'software/qiime-1.4.0-release/bin/check_id_map.py -m /home/qiime/sample/Fasting_Map.txt -o /home/qiime/sample/mapping_output -v';
$a = exec($script);
If you are not on windows, have you tried adding 2>&1 (redirect stderr to stdout) to the end of the command?
$output = system("python /software/qiime-1.4.0-release/bin/check_id_map.py -m /home/qiime/sample/Fasting_Map.txt -o /home/qiime/sample/mapping_output -v 2>&1", $exitcode);
Found from http://www.php.net/manual/en/function.system.php#108713
Also the doc says that it
Returns the last line of the command output on success, and FALSE on
failure.
So if you are trying to get multiple lines, you may need to redirect it to a file and read that in.
instead of system() try surrounding the code in `ticks`...
It has a similar functionality but behaves a little differently in the way it returns the output..

How to change the shell that is used with shell_exec in PHP?

I'm using Windows and I would like to set up PHP so as to use bash shell (installed on my machine thanks to msysgit). The reason is that I have Windows on my development machine and Linux on the production machine.
Thanks!
You can't. But you can launch your own shell like c:\mingw\bin\sh -c 'set'. That would require Mingw on your machine of course.
Using Cygwin it would be:
shell_exec("C:\cygwin\bin\bash.exe --login -c '/cygdrive/c/cygwin/bin/convert.exe --version'");
There is a limit on command line length, therefore I wouldn't recommend putting the script itself there if it is more than just a one-liner. The options just after the shell script interpreter's name, are for the interpreter. If you want to pass arguments to the script the interpreter executes, it needs to be inside the quotes, i.e. just after ".exe" in the convert example.
You can pipe a script to the shell script interpreter also. That way, you wouldn't need to write it to a file, and you could still use a long script.
I.e.:
$ echo "echo 'Hello world';" | sh
Hello world
See http://php.net/manual/en/function.proc-open.php for how to use pipes in PHP.

Understanding a bourne shell script

I came across a wrapper script to run php as fastcgi, could someone explain what is going on in the script?
#!/bin/sh
exec /usr/bin/php5-cgi -c /etc/php5/cgi/php-fcgi.ini
The first line (sha bang or hash bang) set the interpreter, in this case /bin/sh the standard shell, this is not necessarily a bourne shell.
The script is probably used to start a php-cgi session with a custom config file.
The -c flag is used to select a configuration file . .
try /usr/bin/php5-cgi --help for more information on available flags.
Not much. It executes /usr/bin/php5-cgi with the parameter -c /etc/php5/cgi/php-fcgi.ini, i.e. it specifies a special configuration file.
The script replaces itself (exec), keeping the pid, by executing usr/bin/php5-cgi -c /etc/php5/cgi/php-fcgi.ini
If it did not use exec, whoever called it would not get the process ID of the php5-cgi process. Likely this script is intended as a drop-in replacement for executing php directly, and whatever uses this script needs the process ID of the php process.

Bizarre php shell script behavior

It may help if you are on a Mac and use TextMate, though not entirely necessary.
My php location:
$which php
/opt/local/bin/php
The script:
#!/opt/local/bin/php
<?php
shell_exec("echo -n 'my-string' > out.txt");
?>
The -n to echo suppress the newline that is automatically added to all shell echo commands.
If I run the above php code from the shell:
chmod u+x myfile.php
./myfile.php
I end up with 'out.txt', the contents of which being:
-n my-string
If I run the exact same code within TextMate, the contents of 'out.txt' will be correct:
my-string
I can't figures out what php is up to with putting the literal string '-n' in the output. I really wonder why TextMate does the correct thing. I have checked that both are using the same php, php -i shows mostly the same stuff, of course there are differences as one is run within TextMate, the other in the shell, so one output has pointers to the file whereas the other doesn't. As far as I can tell, $PATH and $ENV are the same.
I have tried a handful of different methods to work around this none of which are working. I actually will not be able to use a workaround, as this has been distilled down to a simple case for posting to SO. My use case for this pipes to pbcopy, which I believed was a Mac OS X only feature, so I used >> redirection here because that is universal.
Ultimately, I want a result on my clipboard that does not have a trailing newline, which is dangerous as pasting that in a shell will execute whatever preceded it.
Thanks
Is it possible that php is calling a different echo than your shell built in echo? Many versions of echo do not support -n and will output it as part of your string.
You could try shell_exec("which echo"); to find out which it is running.
Note that printf will not display the new line unless you explicitly add it. So you can use which printf to figure out where this resides and call it instead.
shell_exec("/usr/local/bin/printf '%s' 'mystring' > out.txt");
PHP just defers the call to popen in Unix platforms. See the manual page for Mac OS X:
The command argument is a pointer to a null-terminated string containing a shell command line. This
command is passed to /bin/sh, using the -c flag; interpretation, if any, is performed by the shell.
So it should be the same as running /bin/sh -c "echo -n 'my-string' > out.txt"

Categories