1.11M Members

Invoking a shell script

 
0
 

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"

 
0
 

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);

?

 
0
 

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

 
0
 

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>";
?>
 
0
 

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

 
0
 

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

 
0
 

Sure, check my example above

 
0
 

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

Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article