How to raise and take care of shell scripts: DOS files

Files coming from other systems

Actually this is a special version of the input chapter. But I've read and answered very oft questions concerning files from other systems like WinDOS or (extremly rare) MacOS, that's why here is a chapter dedicated to this problem. The main reason for errors is the end of line sign. Every operating system family handles the EOL in his own way, none of the OS is able to handle EOL's coming from other systems without tool help:

Funny, isn't it? And today very outdated - these codes are originally telex / typewriter codes: "linefeed" - the paper moves one line forward, "carriage return" - the sledge moves back to the left. But we have to live with it.

So, if you write your scripts using Notepad and are astonished about "bad interpreter" messages after the upload or if your 1,000 times tested sed command produces a lot of botch when it's executed on the destination system you should take a careful look at the file format of your scripts and data files. One command to do it is od:

  # a Unix End Of Line looks like this:
  jan@jack:~/tmp> od -cx crlf_test.sh
  0000000   #   !       /   b   i   n   /   b   a   s   h  \n   2   .
          2123 2f20 6962 2f6e 6162 6873 320a 202e
  # and this is a WinDOS End Of Line:
  jan@jack:~/tmp> od -cx crlf_test.sh
  0000000   #   !       /   b   i   n   /   b   a   s   h  \r  \n   2   .
          2123 2f20 6962 2f6e 6162 6873 0a0d 2e32

A very simple tool to fix such "missunderstandings" is tr:

  tr -d '\r' <crlf_test.sh >crlf_test.sh.bak && mv crlf_test.sh.bak crlf_test.sh