Printed from •

Line Endings for Script Files

There are three styles of line endings for text files: Unix/Mac OSX, Mac OS9, and DOS/Windows. Mac OS9 text files place a "carriage return" at the end of each line of text, by default. Unix/Mac OSX text files place a "line feed" at the end of a line, and DOS/Windows text files place both a "carriage return" and a "line feed" at the end of a line.

If you are editing a CGI script that will be installed on a Unix/Linux server, the script must be saved with Unix/Mac OSX line endings, or it may not run. To configure your text editor to save with Unix line endings, see Text Editors for HTML and Script Editing.

If your text editor can't save a file with Unix line endings, you can save the file as text, upload it to the Web server, and then convert it to Unix line endings on the server later. To do this, you will need to use a terminal (command line) program to log in to the server. See How to Log In to With a Command Line Interface so you can log in, and then follow the instructions below.

How to convert a file on the server to Unix line endings using a command line interface

In this example we will convert a script file called "bnbform.cgi" to Unix line endings:

  1. After you have logged in to with a terminal (command line) program, you need to navigate from your home directory (where you start at when you first log in to the server) to the directory that contains your script. Type:
    cd public_html/cgi-bin/bnbform
    and hit return.
  2. Now you need to change the file formatting of the CGI script ("bnbform.cgi") to work on Unix. If you had edited the file on a Windows machine, type:
    dos2unix bnbform.cgi
    and hit return. If you had edited the file on a Macintosh, type:
    mac2unix bnbform.cgi
    and hit return.
  3. The dos2unix or mac2unix command may also change the file's permissions, so you now need to change the permissions of the script to be executable. You can use your FTP program to change permissions, as shown above, or you can do it directly from the command line. If you want to change permissions using the command line type:
    chmod 755 bnbform.cgi
    and hit return.