Jon
Jon

Reputation: 2584

Zipping a file in bash fails

I have this line of code in a file called backup.sh, located in /backup (so the path is /backup/backup.sh)

The code is:

#!/bin/bash
zip -r /backup/Backup-$(date +%Y-%m-%d) /ftb

The file has permissions 777. However, it errors with:

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

/backup and /ftb both exist. I'm running this as a root user.

Upvotes: 1

Views: 78

Answers (1)

John1024
John1024

Reputation: 113834

Run dos2unix or similar utility on it to remove the carriage returns (^M).

This message indicates that your file has dos-style lineendings:

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

Utilities like dos2unix will fix it:

 dos2unix <backup.bash >improved-backup.sh

Or, if no such utility is installed, you can accomplish the same thing with translate:

tr -d "\015\032" <backup.bash >improved-backup.sh

As for how those characters got there in the first place, @MadPhysicist had some good comments.

Upvotes: 3

Related Questions