用于备份目录的 bash 脚本

用于备份目录的 bash 脚本

我正在编写一个脚本,用于按用户备份目录并备份特定天数。我是 Bash 新手,因此修复可能非常简单,但我看不懂。当我运行它时,返回“ syntax error near unexpected token ')'

#
#!/bin/bash
SCRDIR = ${1? Error: No directory given}
DATE =5
echo "Backing up $SCRDIR for $DATE days"

TODAY = "$(date +%Y_%m_%d)"
FILENAME = $SCRDIR"_"$TODAY".tar"
tar -cvfz $FILENAME $SCRDIR
find $PWD -mtime +$DATE -type f -delete
echo "Done"
#

答案1

错误太多了,我不得不使用shellcheck

walt@bat:~(1)$ cat >foo.sh
#!/bin/bash
SCRDIR = ${1? Error: No directory given}
DATE =5
echo "Backing up $SCRDIR for $DATE days"

TODAY = "$(date +%Y_%m_%d)"
FILENAME = $SCRDIR"_"$TODAY".tar"
tar -cvfz $FILENAME $SCRDIR
find $PWD -mtime +$DATE -type f -delete
echo "Done"
walt@bat:~(0)$ shellcheck foo.sh

In foo.sh line 2:
SCRDIR = ${1? Error: No directory given}
       ^-- SC1068: Don't put spaces around the = in assignments.


In foo.sh line 3:
DATE =5
     ^-- SC1068: Don't put spaces around the = in assignments.


In foo.sh line 6:
TODAY = "$(date +%Y_%m_%d)"
      ^-- SC1068: Don't put spaces around the = in assignments.


In foo.sh line 7:
FILENAME = $SCRDIR"_"$TODAY".tar"
         ^-- SC1068: Don't put spaces around the = in assignments.
                     ^-- SC2027: The surrounding quotes actually unquote this. Remove or escape them.


In foo.sh line 8:
tar -cvfz $FILENAME $SCRDIR
          ^-- SC2086: Double quote to prevent globbing and word splitting.
                    ^-- SC2086: Double quote to prevent globbing and word splitting.


In foo.sh line 9:
find $PWD -mtime +$DATE -type f -delete
     ^-- SC2086: Double quote to prevent globbing and word splitting.

相关内容