Operating System - HP-UX
1748223 Members
4733 Online
108759 Solutions
New Discussion юеВ

Re: Carriage Return in perl scripts

 
Peter Brimacombe
Frequent Advisor

Carriage Return in perl scripts

I used scp to copy a perl script from linux to HP-UX and noticed ^M on every line.

How come? I assumed that linux follows the unix convention where each line is terminated by line feed. Where does the carriage return come from?

6 REPLIES 6
Bill Hassell
Honored Contributor

Re: Carriage Return in perl scripts

It comes from a PC where the end-of-line is two characters (CR+LF). It is so pervasive that HP-UX has dos2ux to remove the CR (^M in vi or cat -v):

dos2ux myscript.dos > myscript.perl


Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: Carriage Return in perl scripts

> [...] Where does the carriage return come
> from?

If my psychic powers were not so pathetically
weak, then I might be able to determine
whence this file ("perl script") came, and
that might provide a clue as to the reasons
for its line endings being what they are.
(Whatever they are.)

> [...] and noticed ^M on every line.

Noticed how? How did the lines end on the
original file (before the transfer)?

> I used scp [...]

As usual, showing actual commands with their
actual output can be more helpful than vague
descriptions or interpretations.

> [...] linux [...]

uname -a
ssh -V

> [...] HP-UX [...]

uname -a
ssh -V

Even a few basic facts might help more than
approximately none.
James R. Ferguson
Acclaimed Contributor

Re: Carriage Return in perl scripts

HI Peter:

As Bill noted, Windows (and a few other platforms) delimit lines in text files with a carriage-return/linefeed combination (CRLF) as opposed to UNIX's simple linefeed (LF). There is nothing here that is special/indigenous to the fact that you are dealing with a Perl script.

A common mistake is to FTP a file in binary mode from a Windows to UNIX server. In this case, like 'scp', any carriage return characters are transferred intact. If you use FTP's ASCII mode, carriage-return characters will be added or subtracted to match the receiving platform's standard.

A good survery of the climate can be found here:

http://en.wikipedia.org/wiki/Newline

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: Carriage Return in perl scripts

HI (again) Peter:

By the way, aside from using 'dos2ux' on your HP platform you could (of course) do:

# perl -pi.old -e 's/\r$//' file

...which will strip carriage returns from the CRLF pairs, updating your file (script or whatever) in-place and retaining an original file copy as "*.old".

Regards!

...JRF...
Peter Brimacombe
Frequent Advisor

Re: Carriage Return in perl scripts

thanks everyone for your reply

I think I made a mistake.

the file on the linux machine and the HP-UX machine are identical

and the file on the HP-UX machine does not have ^M in it

I just did scp from the linux to HP-UX, back to linux and then back to HP-UX, still no ^M

here's the script if you're curious
Bill Hassell
Honored Contributor

Re: Carriage Return in perl scripts

scp does not have an "ASCII" or "BINARY" option -- all transfers are exact copies. ftp uses the BINARY option to achieve an exact copy.


Bill Hassell, sysadmin