简体   繁体   中英

Bash script adding special characters to the end of any file I create

I am creating a bash script to create backups, however the bash scripts mkdir is naming the folder with some sort of special characters on the end. if I ls the directory the name show up with a ? on the end which I know is terminals way of showing unrecognized special characters.

How do I get my bash script to to create the folder without adding on this special unrecognized character to the end.

Any help is appreciated.

See script below:

#!/bin/bash
mkdir -p "/var/backups/Backup"
mysqldump -u user1 -ptest DB tbl1 > "/var/backups/Backup/tbl1.sql"
DAY=$(date +%Y%m%d%H%M%S)
zip -r /var/backups/bkup-$DAY.zip /var/backups/Backup

cat -A shows the following:

#!/bin/bash^M$
mkdir -p "/var/backups/Backup"^M$
mysqldump -u user1 -ptest DB tbl1 > "/var/backups/Backup/tbl1.sql"^M$
DAY=$(date +%Y%m%d%H%M%S)^M$
zip -r /var/backups/bkup-$DAY.zip /var/backups/Backup

the ^M ( CR ) characters in your cat -A output are the problem: the shell treats only LF as the "end of line" marker, and the preceding CR character becomes part of the previous word. only '/' and '\\0' characters are forbidden in pathnames in POSIX(ish) systems.

you can fix your script with dos2unix or with

vim yourfile.sh
:set ff=unix
:wq

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM