I wrote a 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!
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.
pico, the command line editor, shall be the solution. – João Pimentel Ferreira Apr 23 '16 at 20:09