-1

Restore_DB.sh :-

#!/bin/bash
mysql -u user -ppassword DB_name < /home/A/B/SQL_File.sql

I used the above code to restore a MySQL database from a cron job but I'm getting the bellow error

/usr/local/cpanel/bin/jailshell: /home/A/B/Restore_DB.sh: /bin/bash^M: bad interpreter: No such file or directory

This is the cron job command I used:-

/home/A/B/Restore_DB.sh
chalitha geekiyanage
  • 6,464
  • 5
  • 25
  • 32
  • 1
    possible duplicate of [./configure : /bin/sh^M : bad interpreter](http://stackoverflow.com/questions/2920416/configure-bin-shm-bad-interpreter) – Cees Timmerman Aug 03 '15 at 15:34
  • 1
    Possible duplicate of [Bash script: bad interpreter](https://stackoverflow.com/q/2841593/608639) – jww Oct 19 '18 at 03:32

4 Answers4

5

Try if dos2unix can fix your file:

$ dos2unix /home/A/B/Restore_DB.sh

If dos2unix does not exist yet, you can install it with your distribution's package manager.

The problem is the newline encoding, Windows/DOS encodes newlines differently than Unix.

  • Unix newline sequence: \n (only line feed character)
  • Windows newline sequence: \r\n (2 characters, carriage return and line feed)

See https://en.wikipedia.org/wiki/Newline#Representations

stefreak
  • 1,460
  • 12
  • 30
4

his looks like a problem with different line end encodings on unixoid and MS-Windows like systems.

Use the line ending \n which is native to unixoid systems, not the MS-Windows style. That one holds an extra character which is typically displayed like what you see in the error message (^M).

You can take a closer look at the line in question by using a hexeditor. This allows you to see exactly what non-printable characters are used inside a string.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Can you please give me an example how to use \n? – chalitha geekiyanage Jan 11 '15 at 13:42
  • Just use a normal `Return` which produces a linebreak. You just have to make sure that you told the editor to use normal unixoid line breaks (unix style). Or use some stripping utility like what @stefreak suggested. Though that sounds like overhead to me, considering you just want to type a linebreak :-) Or, as mentioned above, use a hexeditor. It allows both to verify _and_ to change the string. You can easily spot and remove the offending character. It is displayed like this: 0x10 0x13. Here 0x10 is the `\n` and 0x13 is the `\r` which you do not want there. – arkascha Jan 11 '15 at 13:43
2

I just ran into this on OS X and noticed dos2unix is available as a brew formula:

brew install dos2unix
Hahnemann
  • 4,378
  • 6
  • 40
  • 64
0

yum install dos2unix

works like a charm !!!!

jwvh
  • 50,871
  • 7
  • 38
  • 64
  • 2
    While this leads to the right path (but how do you know that `yum` is the right package management tool for the submitter?), a bit more explanation (of the cause and fix) would be helpful. – Ingo Karkat Apr 07 '16 at 12:15