Why does editing a FreeNAS script/text file on a Windows PC cause issues when copied to NAS?

Why does editing a FreeNAS script/text file on a Windows PC cause issues when copied to NAS?

I've seen this come up a couple of times now, so I decided to post a resource about it.

Background:
When people copy a script/text file to their FreeNAS server which was created (or downloaded) on their Windows PC or Mac (v9 and older), the script sometimes fails to run correctly or displays weird characters at the end of each line, i.e. ^M.

This is often due to the fact that UNIX (including Linux and FreeBSD), Windows/DOS, Macs, and other OS's all treat the newline encoding in a text file differently. In computing, a newline, also known as a line ending, end of line (EOL), or line break, is a special character or sequence of characters signifying the end of a line of text and the start of a new line. The actual codes representing a newline vary across operating systems, which can be a problem when exchanging text files between systems with different newline representations.

The concepts of line feed ( LF) and carriage return ( CR) are closely associated, and can be considered either separately or together. In the physical media of typewriters and printers, two axes of motion, "down" and "across", are needed to create a new line on the page. Although the design of a machine (typewriter or printer) must consider them separately, the abstract logic of software can combine them together as one event. This is why a newline in character encoding can be defined as LF and CR combined into one (known variously as CR+LF, CRLF, LF+CR, or LFCR).

More in-depth information about this can be found on Wikipedia.

So, the following operating systems use:

UNIX uses: LF
Windows/DOS use: CR LF
Mac (v9 and older) uses: CR
Mac (OS X and newer) uses: LF

As you can see from above, FreeBSD requires the LF encoding, but both Windows/DOS and Mac (v9 and older) use different encoding. Because of this FreeBSD tries to convert the incorrect newline codes to characters, which then causes scripts to fail.

Please Note: After 20+ years Microsoft have finally updated the windows Notepad program to support extended end-of-lines, please see https://blogs.msdn.microsoft.com/commandline/2018/05/08/extended-eol-in-notepad/ for further details.

So how can I fix this?
Thankfully some text editors allow for this, and the excellent editor which I use (Notepad++) has the ability to define what end-of-line character you need.

Note: To clarify, Notepad++ is a separate 3rd party software package, which is different than the built-in Microsoft Windows Notepad.

To select the correct one, which is UNIX (LF) for FreeNAS, you need to make sure that you go to the Edit menu, then select EOL Conversion, and then select Unix (LF) before you save the file (see screenshot below).

eol-example.png


Hopefully, this will help with any errors in copying scripts/text files over to your FreeNAS server.

What to do if the script still fails?
Well if you have saved it as above and the script still has issues, its most probably due to the coding in the script, and this resource won't help you with that. However, I do recommend that you paste your script into ShellCheck, which is an excellent site for checking out the code of your scripts.

If you found this resource useful (or not) I would be grateful if you could feedback using the rating system!

Yours
Jonathan

Edits:
11-Aug-2017: Corrected out-of-date information re: Macs.
13-Aug-2017: Clarified that Notepad++ is different than Windows Notepad.
10-May-2018: Updated to highlight Windows Notepad changes.
Author
Hazimil
Views
2,323
First release
Last update
Rating
5.00 star(s) 2 ratings

More resources from Hazimil

Latest reviews

Worked with various editors and never knew this. Good job!
Plain and simple to the point.
Top