syntax error near unexpected token `newline' or `$'\r''

Common errors in running an unix shell scripts are 

line 34: syntax error near unexpected token `newline'
line 30: syntax error near unexpected token `$'\r'' 

Solution:

Unix script file name example.sh then the below is the command to fire on machine.

dos2unix example.sh example.sh

So that it removes the hidden Windows Characters from Files.

While editing files on a machine running some form of Windows and uploading them to a Linux server is convenient, it can cause unforeseen complications. 

Windows-based text editors put special characters at the end of lines to denote a line return or newline. 

Normally harmless, some applications on a Linux server cannot understand these characters and can cause the service to not respond correctly. 

There is a simple way to correct this problem: dos2unix.

What do the hidden characters look like?

Below is an example from the example.sh file showing what the hidden characters look like:

;;;;;;;;;;;;;;;;;;;^M
; Resource Limits ;^M
;;;;;;;;;;;;;;;;;;;^M
;^M
max_execution_time = 30^M
max_input_time = 60^M
memory_limit = 32M^M

As said before, these characters will not appear in most Windows text editors, but will appears in Linux command line utilities like cat or vi. Removing them is rather painless. Just run the command dos2unix.

root@host [~]# dos2unix example.sh example.sh

In the above example of the example.sh, it will look something like this:

root@host [~]# dos2unix example.sh example.sh
dos2unix: converting file
example.sh to UNIX format ...

That’s it. If the command has worked correctly, the file in question shouldn’t have any of the hidden characters in it, like the follow:

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;
;
max_execution_time = 30
max_input_time = 60
memory_limit = 32M

The dos2unix command is a simple way to make sure that files that have been edited and uploaded from a Windows machine to a Linux machine work and behave correctly.



  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP