1,105,416 Community Members

Invoking a shell script

Member Avatar
Dani
The Queen of DaniWeb
20,566 posts since Feb 2002
Reputation Points: 1,356 [?]
Q&As Helped to Solve: 931 [?]
Skill Endorsements: 204 [?]
Administrator
Featured
Sponsor
 
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"

Member Avatar
big_k105
PFO Founder
349 posts since May 2003
Reputation Points: 11 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
Team Colleague
 
0
 
Member Avatar
Dani
The Queen of DaniWeb
20,566 posts since Feb 2002
Reputation Points: 1,356 [?]
Q&As Helped to Solve: 931 [?]
Skill Endorsements: 204 [?]
Administrator
Featured
Sponsor
 
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);

?

Member Avatar
DanceInstructor
Posting Whiz
355 posts since Feb 2005
Reputation Points: 19 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
RamiroS
Junior Poster in Training
57 posts since Mar 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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>";
?>
Member Avatar
mehra
Newbie Poster
1 post since Nov 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
RamiroS
Junior Poster in Training
57 posts since Mar 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Sure, check my example above

Member Avatar
carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
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?

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