2

I have the following prompts

[/share/registrazioni/Script] # cat delete_7gg.sh
#!/bin/sh
find /share/registrazioni/ -type f -mtime +7 -delete
[/share/registrazioni/Script] # which sh
/bin/sh
[/share/registrazioni/Script] # chmod +x delete_7gg.sh
[/share/registrazioni/Script] # ./delete_7gg.sh
-sh: ./delete_7gg.sh: /bin/sh^M: bad interpreter: No such file or directory

why do I get /bin/sh^M: bad interpreter: No such file or directory ? I made the script with vi. I made the script in a qnap qts 4.3.6 that I can reach via ssh. The script should simply delete files older than seven days. I can't install dos2unix because the system has no package manager. I just expected to make a script and put it into cron. but I'm receiving that error instead.

Malkavian
  • 29
  • 1
  • 4
  • 1
    Related: [shebang line not working with cr-lf](https://unix.stackexchange.com/questions/108588/shebang-line-not-working-with-cr-lf) – steeldriver Apr 16 '19 at 12:38
  • 3
    ... since you are using `vi`, you can change between line endings using `:set ff=unix` and `:set ff=dos` – steeldriver Apr 16 '19 at 12:54
  • there are a lot of questions about that CR if you google for `^M` [What is `^M` and how do I get rid of it?](https://unix.stackexchange.com/q/32001/44425), [Unix script appends ^M at end of each line](https://stackoverflow.com/q/15020883/995714), [-bash: ./my_script: /bin/bash^M: bad interpreter: No such file or directory](https://stackoverflow.com/q/14219092/995714)... – phuclv Apr 16 '19 at 15:53
  • I'm voting this down because it's a duplicate as @phuciv shows. – Monty Harder Apr 16 '19 at 16:59
  • Regarding your recent edit to this old question: The second question marked as a duplicate contains answers that do not use `dos2unix`. – Kusalananda Oct 28 '19 at 15:05

1 Answers1

10

You seem to have Windows-style line endings (CRLF, ^M^J) instead of unix-style line endings (LF, ^J). Try dos2unix

dos2unix delete_7gg.sh

then run as usual

From man:

dos2unix - DOS/MAC to UNIX text file format converter

Siva
  • 9,017
  • 8
  • 56
  • 86
  • thank you for the answer but I made the script in a qnap qts 4.3.6 that I can reach via ssh. It should simply delete files older than seven days. I can't install dos2unix because the system has no package manager. I just expected to make a script and put it into cron. but I'm receiving that error instead. – Malkavian Oct 28 '19 at 14:48
  • @Malkavian Another way: `tr -d '\r' < input > output`. – Christopher Oct 28 '19 at 14:59