1

I'm running a few repetitive tasks and I think will be good to create a small bash script to perform all them in just one command: run the bash script. This are the tasks I need to run:

# stop HTTPD, clear logs, start HTTPD back
service httpd stop
cd /var/log/httpd/
rm -r *
service httpd start

# clear PHP error logs
truncate /var/log/php/php_errors.log --size 0

# clear MySQL error, query and slow logs
truncate /var/log/mysql/mysql_error.log --size 0
truncate /var/log/mysql/mysql_query.log --size 0
truncate /var/log/mysql/mysql_slow.log --size 0

So this is how I am doing:

#! /bin/bash

# stop HTTPD, clear logs, start HTTPD back
service httpd stop
cd /var/log/httpd/
rm -r *
service httpd start

# clear PHP error logs
truncate /var/log/php/php_errors.log --size 0

# clear MySQL error, query and slow logs
truncate /var/log/mysql/mysql_error.log --size 0
truncate /var/log/mysql/mysql_query.log --size 0
truncate /var/log/mysql/mysql_slow.log --size 0

But I am getting this error all the time and I don't know what I am doing wrong:

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

What I am doing wrong and/or missing?

  • Additionally to the ^M problem pointed in the answer, delete space after ! in shebang. – jimmij Aug 24 '15 at 1:43
2

You need to run dos2unix on your file, which is presumably copied into your Linux distribution with Windows file endings. The ^M, aka the carriage return character, trips your Linux distribution into thinking it needs to look for a command bash^M, instead of bash.

To actually view the ^M characters, you can open your file with cat -A:

$ cat -A file
# --- comment: expected output below ---
#! /bin/bash^M
# stop HTTPD, clear logs, start HTTPD back^M
service httpd stop^M
cd /var/log/httpd/^M

In case you do not have dos2unix installed, you can also try replacing using the tr command (as explained here):

tr -d '\r' < inputfile > outputfile
  • 1
    Thx that fixes the issue on the script and point me on the right direction to configure my IDE to avoid the windows file endings – ReynierPM Aug 24 '15 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.