468,772 Members | 2,128 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,772 developers. It's quick & easy.

system() returns the last line of output twice

Hi,

I am running php v 5.0.0 on a redhat box. I have a php script in a
file called p_script_1. I have another small script in file
p_script_2. p_script_1 calles p_script_2 through a system() call. THe
last line of the output from p_script_2 is duplicated. As a test I ran

php -E 'echo system ("ls ");'

<you have to hit ^D after you hit newline>. Anyway, this simple "ls"
is also duplicating the last line of output. Is this a bug or feature?
Oh, just got access to a brand new, up to date RedHat box running php
5.1.4 and it is happening there too.

Jun 23 '06 #1
3 2621
seven.reeds wrote:
Hi,

I am running php v 5.0.0 on a redhat box. I have a php script in a
file called p_script_1. I have another small script in file
p_script_2. p_script_1 calles p_script_2 through a system() call. THe
last line of the output from p_script_2 is duplicated. As a test I ran

php -E 'echo system ("ls ");'

<you have to hit ^D after you hit newline>. Anyway, this simple "ls"
is also duplicating the last line of output. Is this a bug or feature?
Oh, just got access to a brand new, up to date RedHat box running php
5.1.4 and it is happening there too.

ls already emits to STDOUT.

So, you have the output from the 'ls' command and then 'echo' echoing
the last line.

try php -E 'system("ls");' to see the difference.

To see what is really happening, try this:
php -E 'echo -->.system("ls");'

-david-

Jun 23 '06 #2
Doh! thanks David. ... why didn't i try that? sigh

Jun 23 '06 #3
On Fri, 23 Jun 2006 16:05:46 -0400, David Haynes wrote:
php -E 'echo system ("ls ");'

<you have to hit ^D after you hit newline>. Anyway, this simple "ls" is
also duplicating the last line of output. Is this a bug or feature?

ls already emits to STDOUT.

So, you have the output from the 'ls' command and then 'echo' echoing the
last line.


You're *ALMOST* right. ls emits the output to STDOUT, but that is trapped
by PHP when running system. PHP then prints the output of the command
when using the "system" function call and returns the last line (which the
OP then echoes).

But the point is, it's the usage of system that's the problem, not ls.

http://uk.php.net/system

If you really want to run the command and only get the last line, you're
better using exec (which is the same as system, but doesn't print the
output, automatically returns the last line and can optionally return the
full output in an array).

http://uk.php.net/exec

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Penn Markham | last post: by
1 post views Thread by Randy Jackson | last post: by
1 post views Thread by Hifni Shahzard | last post: by
8 posts views Thread by Oski | last post: by
4 posts views Thread by sebastien.willemijns | last post: by
30 posts views Thread by Yorian | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.