We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,477 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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"

6
Contributors
8
Replies
2 Years
Discussion Span
5 Years Ago
Last Updated
10
Views
Dani
The Queen of DaniWeb
Administrator
21,555 posts since Feb 2002
Reputation Points: 1,555
Solved Threads: 376
Skill Endorsements: 124
big_k105
PFO Founder
Team Colleague
362 posts since May 2003
Reputation Points: 36
Solved Threads: 2
Skill Endorsements: 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);

?

Dani
The Queen of DaniWeb
Administrator
21,555 posts since Feb 2002
Reputation Points: 1,555
Solved Threads: 376
Skill Endorsements: 124

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

DanceInstructor
Posting Whiz
368 posts since Feb 2005
Reputation Points: 17
Solved Threads: 14
Skill Endorsements: 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>";
?>
RamiroS
Junior Poster in Training
57 posts since Mar 2005
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 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

mehra
Newbie Poster
1 post since Nov 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
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

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

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0830 seconds using 2.8MB