system() returns the last line of output twice

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • seven.reeds

    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.

  • David Haynes

    #2
    Re: system() returns the last line of output twice

    seven.reeds wrote:[color=blue]
    > 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.
    >[/color]
    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-

    Comment

    • seven.reeds

      #3
      Re: system() returns the last line of output twice

      Doh! thanks David. ... why didn't i try that? sigh

      Comment

      • Andy Jeffries

        #4
        Re: system() returns the last line of output twice

        On Fri, 23 Jun 2006 16:05:46 -0400, David Haynes wrote:[color=blue][color=green]
        >> 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?
        >>[/color]
        > ls already emits to STDOUT.
        >
        > So, you have the output from the 'ls' command and then 'echo' echoing the
        > last line.[/color]

        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.

        Execute an external program and display the output


        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).



        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

        Comment

        Working...