Invoking a shell script
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"
Related Article: shell_exec problem
is a PHP discussion thread by fernandodonster that has 5 replies and was last updated 5 years ago.
Dani
The Queen of DaniWeb
21,555 posts since Feb 2002
Reputation Points: 1,555
Solved Threads: 376
Skill Endorsements: 124
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);
?
Dani
The Queen of DaniWeb
21,555 posts since Feb 2002
Reputation Points: 1,555
Solved Threads: 376
Skill Endorsements: 124
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>";
?>
RamiroS
Junior Poster in Training
57 posts since Mar 2005
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
Sure, check my example above
RamiroS
Junior Poster in Training
57 posts since Mar 2005
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0