I'm trying to make a simple shell script to backup files on the same machine but I keep running into errors I don't know how to resolve [duplicate]

I wanted to execute a shell script:

-rwxr-x--x 1 root root   17234 Jun  6 18:31 create_mgw_3shelf_6xIPNI1P.sh

I tried to do a standard procedure, but I got this error:

./create_mgw_3shelf_6xIPNI1P.sh 
localhost 389 -l /opt/fews/sessions/AMGWM19/log/2013-06-06-143637_CLA-0 
DEBUG   cd/etc/opt/ldapfiles/ldif_in ;
./create_mgw_3shelf_6xIPNI1P.sh 
localhost 389 -l /opt/fews/sessions/AMGWM19/log/2013-06-06-143637_CLA-0
**ERROR  sh: ./create_mgw_3shelf_6xIPNI1P.sh: /bin/bash^M: bad interpreter: No such file or directory**

What does it mean? I was doing this as the root user under the root group.

Does it mean that the file does not have the correct permission for the root user?


This isn't a permission issue, you aren't getting a message about permissions

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

The script indicates that it must be executed by a shell located at /bin/bash^M. There is no such file: it's called /bin/bash.

The ^M is a carriage return character. Linux uses the line feed character to mark the end of a line, whereas Windows uses the two-character sequence CR LF. Your file has Windows line endings, which is confusing Linux.

Remove the spurious CR characters. You can do it with the following command:

sed -i -e 's/\r$//' create_mgw_3shelf_6xIPNI1P.sh

In vim you could also use :set ff=unix and then save the file, or :set ff=dos to get DOS formatting again.


Your file has DOS/Windows style line endings (CR LF), but on Unix-like systems only the LF control character is used as line break.

The additional CR control character is shown encoded as ^M in your output. You can also see it when you run cat -A create_mgw_3shelf_6xIPNI1P.sh.

To convert the line endings from DOS/Windows style to Unix style, there's a tool called dos2unix. You install it using:

sudo apt-get install dos2unix

Then you can simply convert files' line endings in both ways using

dos2unix FILENAME
unix2dos FILENAME

In your case, simply run this command below and the script file will be converted in-place:

dos2unix create_mgw_3shelf_6xIPNI1P.sh

After that Bash should be able to interpret the file correctly.


The Problem ist you edit with Dos!

open your file with vi then set unix with:

:set ff=unix
:wq

and it all fine