php using shell commands

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

php using shell commands

Post by sheepz »

i'm using a script that was an example in php.net somewhere on how to use "shell_exec()" basically i want to exec shell commands and capture the value and display it on the site

Code: Select all

<?
  $result = shell_exec("date");
  echo $result;
?>
getting this error:

Warning: shell_exec() [function.shell-exec]: Unable to execute 'date' in c:\Inetpub\www\shell.php on line 2

not sure what's going on =/
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

As you are using windows maybe try using exec() instead.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

The problem is that "date" seems to be a built-in function of cmd.exe

I believe there is a "date.exe" athttp://sourceforge.net/projects/unxutils which you can use.
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

I'm sorry for the lack of information, yes I am using WinXP SP2 with IIS6. i've tried using the exec() i'm not sure if i used it correctly

<?
$result = exec('date');
echo $result;
?>

still generating errors


Should the "date" work if it's a built in function of cmd.exe? I thought that's how it should work, if it's a built in function, therefore it would execute. thanks for the help =)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you want to run stuff through the command line interpreter, cmd.exe to run it.

http://www.ss64.com/nt/cmd.html
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

i'm sorry, i'm really new, don't understand exactly. should the code be something like this?

<?
$result = shell_exec("cmd.exe date");
echo $result;
?>

i'm pretty sure thats wrong =(
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Ermm... why aren't we just using date()?

The stdin might also be a problem. On XP, after invoking date, the command line asks for a new date. Could that be the problem?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

although I also wonder why sheepz isn't using date() as well, there is

Code: Select all

cmd /c"echo %date%"
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

oh i didn't know there was a function called date() i'm gonna look that up in php.net. also i want to learn how to run cmd commands thru php. i saw a vague description wich used that little

$result = shell_exec("date");

didn't explain anything much just said it would use the command interperter and display the date. was searching the foras.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Mm... unlike Perl, which encourages the use of shell calls to get things done, PHP has internal functions that should be used to do about anything.
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

i'm sorry i must be asking the question wrong but i would like to see an example of using php to capture information from a shell command and display it thru php. i got this from php.net

<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>

the "ls -lart" command is clearly for linux so i tried to change it to fit windows

<?
$result = shell_exec("%date%");
echo "$result";
?>

i would like to run shell commands, use the variable to hold the string and then display it but can't seem to get the syntax correct.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Actually, you're barking up the wrong tree. ;-)

Check this out:

Code: Select all

C:\Documents and Settings\Edward>%date%
'Thu' is not recognized as an internal or external command,
operable program or batch file.
The attempt of accessing the variable works, it's just that the shell is now trying to execute it. Use echo and...

Code: Select all

C:\Documents and Settings\Edward>echo %date%
Thu 04/13/2006
Voila! The corresponding PHP code is:

Code: Select all

<?php
$output = `echo %date%`;
?>
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

<?php
$output = `echo %date%`;
echo $output;
?>

I did a cut and paste of and just added the echo $output; but the page displays nothing. sorry i probably misunderstanding, this is called the "backtick" fucntion yes?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Works for me. :? Yes, this is backticks. Are you sure PHP is installed correctly and has access to shell?
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

i'm not sure if my php has access to shell. everything should be on default. how would i check? do i change the php.ini file?
Post Reply