我正在处理的 bash 脚本有问题,我几乎是 Bash 脚本的新手,并且已经在这个问题上工作了几个小时,但无法弄清楚问题可能是什么。
当我运行脚本时,这是我得到的错误:
backupscripts.sh: line 55: conditional binary operator expected
backupscripts.sh: line 55: syntax error near `!=0'
backupscripts.sh: line 55: ` if [[ $? !=0 ]]; then echo "Problems Copying Backup File to Backup Host;" fi'
希望您能帮我指出问题所在,谢谢。
这是脚本:
#!/bin/bash
BACKUPDIR=~/backup
SCRIPTDIR=~/arespaldar
BACKUPFILE=/backup.$(date +%F).bz2
BACKUPHOST=199.21.112.70
COUNT=$(ls $BACKUPDIR | wc -l)
TRESHOLD=7
function checkbackupdir() {
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
}
function backup() {
if [[ $COUNT -le $THRESHOLD ]]
then
tar -cjvf $BACKUPDIR/$BACKUPFILE $SCRIPTDIR
if [[ $? -ne 0 ]]; then echo "Poblems Creating Backup File;" fi
scp $BACKUPDIR/$BACKUPFILE $BACKUPHOST:
if [[ $? !=0 ]]; then echo "Problems Copying Backup File to Backup Host;" fi
}
checkbackupdir
backup
我已建议进行更改,现在当我运行脚本时出现此错误:
backupscripts.sh:第 34 行:意外标记('
backupscripts.sh: line 34:
foo checkbackupdir () {'附近的语法错误
这是第 34 行
foo checkbackupdir () {
这是完整的脚本,对其进行了更改:
#!/bin/bash
# Author: Chris Navarrete
# Date: 16.06.2013
# Purpose: Used to backup files and/or directories locally and store remotely
# THIS LINES BELOW ARE THE VARIABLES
BACKUPDIR=~/backup
SCRIPTDIR=~/arespaldar
BACKUPFILE=/backup.$(date +%F).bz2
BACKUPHOST=199.21.112.70
COUNT=$(ls $BACKUPDIR | wc -l)
TRESHOLD=7
foo checkbackupdir () {
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
}
foo backup () {
if [[ "$COUNT" -le "$THRESHOLD" ]]
then
tar -cjvf "$BACKUPDIR"/"$BACKUPFILE" "$SCRIPTDIR"
if [[ $? -ne 0 ]]; then echo "Poblems Creating Backup File;" fi
scp "$BACKUPDIR"/"$BACKUPFILE" "$BACKUPHOST":
if [[ $? -ne 0 ]]; then echo "Problems Copying Backup File to Backup Host;" fi
fi
}
checkbackupdir
backup
#END
关于什么可能仍然导致此错误的任何想法?
谢谢你们的帮助。