By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

system() returns the last line of output twice

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
Doh! thanks David. ... why didn't i try that? sigh

Jun 23 '06 #3

P: n/a
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.