-1

I would like to run the bash script in CentOS 7 in terminal to export database dump. However, it ends with error.

backup_test.sh

#!/bin/bash

thisthing=fiveminute
dumpfile=db-${thisthing}-`date +%Y%m%d%H%M`.sql
mysqldump database_name > $dumpfile.tmp 
mv $dumpfile.tmp $dumpfile

I have setup my ~/.my.cnf to be able to run dump without asking for user password:

.my.cnf

[mysqldump]
user=username
password=password
skip-extended-insert
skip-dump-date

Execution from terminal

[site.backup@localhost ~]$ ./backup_test.sh

This ends up in following error:

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

Can anyone please guide where I am going wrong??

Edit #1

Permissions are set properly as backup_test.sh is set to be executable

Solution

The problem was due to the Windows based line endings, \r, guess I copied the script from Windows based file into the Centos file, hence the error.

To change the script from Windows encoding to Unix in vim you could also use :set ff=unix and then save the file, or :set ff=dos to get DOS formatting again.

Ghazanfar Mir
  • 3,493
  • 2
  • 26
  • 42

1 Answers1

1

You have windows end of line characters (\r) in your shebang line (that's the ^M) part. Run dos2unix on your script and you should be good.

If you don't have dos2unix the following will work with GNU sed (should be default on CentOS 7):

sed -i 's/\s*$//' backup_test.sh

which will remove the whitespace (including \r) from the end of each line, so don't do this if there is important whitespace at the end of lines (which does not seem to be the case with your script)

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67