LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-27-2004, 09:35 AM   #1
zovres
Member
 
Registered: Sep 2002
Posts: 184

Rep: Reputation: 30
php: output the result of exec( $command )


I want to develop a console for php and I wanted to know how to retreive and output the result of a unix command.
At first I outputed the command to a file and read the file with php but some commands don't allow the "> filename" option.

is there another way to do that?

thx
 
Old 07-27-2004, 10:05 AM   #2
cjp
Member
 
Registered: Dec 2003
Location: the Netherlands
Distribution: SuSE 9.0
Posts: 54

Rep: Reputation: 15
Nice idea, a PHP console. I guess it will be like a telnet session in your browser.

For program execution functions, look at http://www.php.net/manual/en/ref.exec.php. I don't know why your "> output" doesn't work. On UNIX systems, the >, < etc. are not parsed by the program, but by the shell, e.g. bash. Maybe you work with PHP on windows??? Maybe your command line is not parsed by a normal shell so that you can't use > ???

Anyway, I think that you need the proc_open and proc_close functions. You can access either input or output with a file descriptor, just like ordinary files.

How are you going to make this interactive? For example when the user hits a character on his keyboard, how are you going to give that key to the PHP script? Does the PHP script run in an infinite loop (which means that the page will never completely be loaded), or does it end (which may mean that the child process also ends)?
 
Old 07-27-2004, 10:12 AM   #3
zovres
Member
 
Registered: Sep 2002
Posts: 184

Original Poster
Rep: Reputation: 30
I was only gonna make a form since it is just for a few commands (and I'm a php newb) if you have a better idea, I'll be glad to hear it

for the > option, try it with the useradd command, on my redhat9 the output of the command is not directed to the file

thx
 
Old 07-27-2004, 10:13 AM   #4
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Rep: Reputation: 30
I suppose you could make it keep running until that command finishes, and then open the page again for another command. Stuff with ncurses might be hard...
 
Old 07-27-2004, 10:55 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
To get the output of a unix commmand inside PHP, the easiest is to use popen(). See http://www.php.net/manual/en/function.popen.php

But this does not let you write to the input and read from the output from the same process. You probably really need this for a web-terminal. To overcome this, you need to use l(ike cjp already said) proc_open(). See http://www.php.net/manual/en/function.proc-open.php

Especially for your idea (the "web-terminal") there's an interesting note in the latter page, referring to PHP5:
Quote:
PHP 5RC2 introduces pty support for systems with Unix98 ptys. This allows your script to interact with applications that expect to be talking to a terminal. A pty works like a pipe, but is bi-directional, so there is no need to specify a read/write mode. The example below shows how to use a pty; note that you don't have to have all descriptors talking to a pty. Also note that only one pty is created, even though pty is specified 3 times. In a future version of PHP, it might be possible to do more than just read and write to the pty.
Having an interface to PTY's essentially means it will give you the same potential an xterm has.
 
Old 08-27-2004, 04:35 AM   #6
shadow_wwp
LQ Newbie
 
Registered: Aug 2004
Location: China
Distribution: Turbo & LFS
Posts: 5

Rep: Reputation: 0
why not do it like this:

$result = shell_exec($command);

echo $result ;

?
 
Old 08-27-2004, 06:41 PM   #7
zovres
Member
 
Registered: Sep 2002
Posts: 184

Original Poster
Rep: Reputation: 30
just so that people know :

what shadow_wwp says works but it's better to append 2>&1 at the end of the command so that even when the command results in an error, it returns the error string.

example:
$result = shell_exec( $command." 2>&1" );
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Output the result of a command to a file darkarcon2015 Linux - Newbie 1 09-24-2005 06:29 PM
BASH Command Result redhatnoob Programming 2 08-19-2004 06:25 PM
php exec devit Programming 4 04-08-2004 03:26 AM
PHP exec Spooky Programming 1 01-19-2004 08:34 AM
exec cgi from php? barazor Programming 5 01-23-2002 09:12 PM


All times are GMT -5. The time now is 12:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration