Thursday, March 19, 2009

.sh: /bin/sh^M: bad interpreter: No such file or directory

Error : -bash: ./run.sh: /bin/sh^M: bad interpreter: No such file or directory

Occurs when : You copy something from windows to linux and try to execute the .sh file in terminal.

Cause : Copying something from Windows to Linux converts 'line terminating characters' (crlf) from Windows to '^M' in Linux. You can see these by typing the command 'vi fileName.sh'
This can also happen to other (than .sh) files as well. So it is a good practice to follow the following solution whenever you are copying from Windows to Linux OR copying from Linux to Windows.
DOS text files traditionally have carriage return and line feed pairs as their newline characters while Unix text files have the line feed as their newline character. fromdos converts text files from the DOS format to the Unix format, while todos converts text files from the Unix format to the DOS format.

Solution : To remove these '^M', take the following action steps

1. run 'dos2unix' command from terminal. (for all .sh files)
$ dos2unix *.sh

2. If dos2unix is not installed, you can install it by
sudo apt-get install tofrodos

3) For other files you can do dos2unix by

find . -name *.java -exec dos2unix '{}' /;


where,
find - is a command to find the files
. - in current directory
*.java - is a file type in the directory from which you are running the command
-exec - execute the command reccurssively
dos2unix - command to be executed on found files
{} - file name format
; - should end with ';', but as ';' special char, prefixed with '/'

1 comment:

ashtaganesh said...

To run dos2unix on ALL files in a directory run following command

for f in `find * -type f`; do echo "dos2unix $f $f"; done | sh

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...