How can I invoke a shell script with parameters from within a PHP script? For example, from within my web-based PHP script, I want to execute the command "/home/cscgal/script.sh -f foo"

I tried reading up but I'm still confused? When is it appropriate to use exec() over shell_exec() over system() ?? Is the only difference what they return / output? The next question that I have is I want to use a php variable as one of my parameters. Is this a problem? Is the appropriate syntax

exec("script.sh -u $username");

or

exec("script.sh -u " . $username);

?

I haven't used these but that is the way I read it, only differences in output. The command part is a string so I assume you can use variables normally in the command.

Dance

I tried reading up but I'm still confused? When is it appropriate to use exec() over shell_exec() over system() ?? Is the only difference what they return / output? The next question that I have is I want to use a php variable as one of my parameters. Is this a problem? Is the appropriate syntax

exec("script.sh -u $username");

or

exec("script.sh -u " . $username);

?

Hi Dani, it is not a problem. Sometimes it is better to put everything in a variable and then exec:

$toexec="sh /etc/" . $program;
$exec=exec($toexec);

The difference between exec, system and shell_exec is the way the output is treated (under Windows at least with PHP < 4.3.x these functions behave strange depending on the machine). You can also use the backtick ooperator:

http://cr.php.net/manual/en/language.operators.execution.php

<?php
$output = `ls -al`;
echo "<pre>$output</pre>";
?>

Hi Dani, it is not a problem. Sometimes it is better to put everything in a variable and then exec:

$toexec="sh /etc/" . $program;
$exec=exec($toexec);

The difference between exec, system and shell_exec is the way the output is treated (under Windows at least with PHP < 4.3.x these functions behave strange depending on the machine). You can also use the backtick ooperator:

http://cr.php.net/manual/en/language.operators.execution.php

<?php
$output = `ls -al`;
echo "<pre>$output</pre>";
?>

my shell script is running from PHP from web browser partially ,means it is creating a temprory file but not executing the gblinkapp,well this run through console

shell script file.....link.sh
----------------------------------------
touch /var/www/html/humanapress/mehra;
/usr/local/src/gblink/gblinkapp/gblinkapp -k>/var/www/html/humanapress/mehra
----------------------------------------

php file test.php
---------------------------------
system('sh link.sh',$rv);
echo $rv;
---------------------------------

if i run this test.php in browser it is showing satus 126 as retnvalue and it does't append the output to tmp file mehra.but the same thing is running from console
as i do
$ php test.php

kindly give solution

can we run unix commands like "cd /usr/local " through php

Sure, check my example above

what about untaring files of .tar.gz extension? consider the file name to be stored in php variable $filename. how will the command be?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.