1

运行 Bash 脚本时出现此错误:

语法错误:文件意外结束

无法真正找到错误在哪里,一直在寻找几个小时,仍然得到这个错误。

这是脚本希望有人不能指出我正确的方向:

#!/bin/bash 


BACKUPDIR=~/backup
SCRIPTDIR=~/respaldar
BACKUPFILE=/respaldo.$(date +%F).bz2
BACKUPHOST=199.21.112.70
COUNT=$(ls $BACKUPDIR | wc -l)
TRESHOLD=7




if [[ ! -e $BACKUPDIR ]]
then
     echo "Creating Backup Directory because it doesn\'t exist !"
    mkdir ~/backup
    COUNT=0
#    exit 0
else
   COUNT=$(ls $BACKUPDIR | wc -l)
fi




if [[ $COUNT -le $THRESHOLD ]]
then
      tar -cjvf $BACKUPDIR/$BACKUPFILE $SCRIPTDIR 
      if [[ $? -ne 0 ]]; then echo "Problems Creating Backup File;"  fi
      scp $BACKUPDIR/$BACKUPFILE $BACKUPHOST:
      if [[ $? -ne 0 ]]; then echo "Problems Copying Backup File to Backup Host;" fi
fi







#END

感谢帮助。

4

1 回答 1

3

我将整个内容复制到并发现与以下内容相同的内容fedorqui

if [[ $? -ne 0 ]]; then echo "Problems Creating Backup File;"  fi
...
if [[ $? -ne 0 ]]; then echo "Problems Copying Backup File to Backup Host;" fi

;之前",应该是之后。

我建议在这两种情况下都使用更短的解决方案:

[ $? -ne 0 ] && echo "Problems Creating Backup File">&2 && exit 1

如果失败,这将退出。甚至更健谈的版本:

tar -cjvf $BACKUPDIR/$BACKUPFILE $SCRIPTDIR || \
    { echo "Problems Creating Backup File">&2;exit 1;}

或者,如果您只想在整个过程失败时看到错误消息:

tar -cjvf $BACKUPDIR/$BACKUPFILE $SCRIPTDIR && \
    scp $BACKUPDIR/$BACKUPFILE $BACKUPHOST: || \
    { echo "Backup failed">&2;exit 1;}
于 2013-06-18T15:04:38.270 回答