0

I have a PHP script run form the command line on an AWS AMI Linux EC2 instance that echos out when it is renaming files, and it is wrapping the end of the string back to the beginning. The string should look something like this:

renaming /var/scripts/_working_files/micros/import_micros_bkp/mytown/emp_def_out.sql to emp_def_out_mytown.sql

but it is coming out like this:

.sqlrenaming /var/scripts/_working_files/micros/import_micros_bkp/mytown/emp_def_out.sql to emp_def_out_mytown

I don't understand why it moves the end of the string to the beginning of the line. Is it something I need to change in bash? I don't think there's anything wrong in PHP. It only appears to happen with strings that have a path in them like the one above.

1
  • 1
    It looks like it was an end of line character issue. The files were created on a Windows computer and the PHP script on Linux was breaking with the /n and /r/n issue. It seems wierd that it would break at that point in the string though.
    – raphael75
    Commented Feb 28, 2017 at 20:34

1 Answer 1

0

It was an end-of-line character issue. The file being processed was created in Windows (using cr lf) but my script was exploding it on "\n". I created a simple function to detect and return which eol character is being used. It's not comprehensive or perfect, but it's good enough for what we need in our environment:

function detect_eol($options){
    /*
    options
        str
    */

    $str = $options['str'];

    $eol = array(
        array('raw' => "\r\n", 'str' => '\r\n'),
        array('raw' => "\n", 'str' => '\n')
    );

    for($x=0;$x<count($eol);$x++){
        $cur_eol = $eol[$x];
        if(strpos($str, $cur_eol['raw']) !== false){
            $retval = $cur_eol;
            break;
        }
    }

    return $retval;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .