语法错误:意外的文件结束

时间:2013-06-18 14:36:59

标签: linux bash unix

运行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

感谢帮助。

1 个答案:

答案 0 :(得分: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;}