此 tar 命令在命令行上有效,但在脚本中失败

This tar command works on command line but fails in a script

此 tar 命令在命令行上有效,但在脚本中失败。

代码:

find ~/ -name "sql*.db" | tar -czv -f backup.tar.gz -T -

命令行输出:

find ~/ -name "sql*.db" | tar -czv -f backup.tar.gz -T -
tar: Removing leading `/' from member names
/home/myuser/folder1/sql1.db
/home/myuser/folder2/sql2.db
/home/myuser/folder3/sql3.db
/home/myuser/folder4/sql4.db
/home/myuser/folder5/sql5.db

脚本错误:

[ps623237]$ sh backup.sh
tar: -\r: Cannot stat: No such file or directory
tar: Error is not recoverable: exiting now

问题是 Windows (CRLF) 而不是 Linux (LF)。我用记事本++

Edit/EOL Conversion/UNIX 

修复它。这里是精炼后的代码。

#!/bin/sh
DOW=$(date +"%A");
find /home/user1 -name sql*.db | tar -czv -f /home/user1/backup/$DOW.DB.tar.gz -T -