Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Some background on the issue:

I am new to scripting and wrote a simple Bash script in Windows using Notepad++.

cxStopAllServicesOnSERVER.sh

#!/usr/bin/bash
cd "/some/path"
echo "Hi. Logs can be found at "`pwd`"/cxStartStopLogger.log"
echo "["`date`"]*** STOPPING ALL SERVICES ON SERVER ***" >> "cxStartStopLogger.log"
exit

Now after uploading it and setting the required file permissions, I tried executing it as follows:

bash-3.00$ cat cxStopAllServicesOnSERVER.sh #Let's have a look at the code.
#!/usr/bin/bash
cd "/some/path/"
echo "Hi. Logs can be found at "`pwd`"/cxStartStopLogger.log"
echo "["`date`"]*** STOPPING ALL SERVICES ON SERVER ***" >> "cxStartStopLogger.log"

bash-3.00$ # Code and hashbang 'looks' correct, 
bash-3.00$ # if there is any issue with the format (EOL characters and such) 
bash-3.00$ # we cannot see it using cat.
bash-3.00$ 
bash-3.00$ sh cxStopAllServicesOnSERVER.sh
cxStopAllServicesOnSERVER.sh[2]: /some/path^M:  not found
Hi. Logs can be found at /some/path/cxStartStopLogger.log
bash-3.00$ # Note that ^M appears at the end of the line.

bash-3.00$ bash -x cxStopAllServicesOnSERVER.sh
+ cd $'/some/path\r'
: No such file or directory1.sh: line 2: cd: /some/path
++ pwd
' echo 'Hi. Logs can be found at /some/path/cxStartStopLogger.log
Hi. Logs can be found at /some/path/cxStartStopLogger.log
++ date
+ echo '[Sun' Nov 18 00:28:17 EST '2012]*** STOPPING ALL SERVICES ON SERVER ***'
bash-3.00$ # Note that '\r' return character appears at the end of the line.

Problem: When I change the code to korn shell I face the same issue. Appears that an incorrect character is getting added at the end of the line.

NOTE: I found the solution and have posted the same as an answer. Feel free to update or improve it to help other beginners that may face the same issue. Thanks!

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

After snooping around various forums and some Stack-overflow post I found the issue.

Here is an explanation to better understand and 'see' the root cause of the errors.

First off if you are writing code on Windows, better arm yourself with Notepad++. It's basically the Swiss Army knife of text-editors in my opinion.

Now to have a look at the EOL (end-of-line) characters and other control symbols do the following:

1.Open the file in Notepad++.

Select View > Show Symbol > Show All Characters from the Menu bar

You should now see the symbols as follows: Screenshot of Notepad++:Editing a UNIX script on Windows

Aha! Windows/MS-DOS uses CR+LF to indicate end-of-lines. Now UNIX uses LF character to indicate line termination (EOL character).

2.1.Let's convert the Windows EOL into UNIX EOLs:

Select EDIT > EOL Conversion > UNIX Format

You should see something like this:

Screenshot of Notepad++: EOL Conversion to UNIX Format This explains why we saw the '\r' character getting appended at the end of the line. This is because UNIX recognized the '\n' or LF (linefeed) as the line termination/EOL character but ignore the '\r' or ^M (carriage return) character.

We can also test for the EOL issue by

bash-3.00$ head cxStopAllServicesOnTNSQ08041.sh | cat -vet | head -1
#usr/bin/bash^M$
bash-3.00$ #We see that ^M, that is \r is found at the end of each line. 

2.2.If you would like convert the file from Windows to UNIX EOL format on UNIX then there are two ways of fixing this:

$ dos2unix myScript.sh

or

Use the solution provided by @Chris Down as below:

$ sed -i 's/\r$//' cxStopAllServicesOnSERVER.sh

References:

  1. For more information on line termination and what-is-the-difference-between-r-and-n refer these links.
share|improve this answer
add comment

Your script contains CRLF line endings, whereas Unix uses LF line endings. The following will remove them, if you have GNU sed:

sed -i 's/\r$//' cxStopAllServicesOnSERVER.sh

If you don't have GNU sed, write to a temporary file, and then move it over the top of the old file.

As an aside, if you invoke a script using sh, you will override the shebang. That's probably not what you want.

share|improve this answer
    
Thanks.. trying it out now. –  Kent Pawar Nov 18 '12 at 5:55
1  
The dos2unix tool is also worth suggesting. –  jordanm Nov 18 '12 at 7:14
    
@jordanm Yup. I covered it in my post below. –  Kent Pawar Nov 18 '12 at 10:18
    
there is no shebang defined. !/ has to be inserted into the first line to have one. –  miracle173 Nov 18 '12 at 11:51
    
@miracle173 I assumed that was a typo rather than being intentional. –  Chris Down Nov 18 '12 at 14:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.