我正在处理我正在处理的bash脚本的问题,我几乎是Bash Scripting的新手,并且已经解决了这个问题几个小时,无法弄清楚问题可能是什么。
当我运行脚本时,这是我得到的错误:
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'
希望你能帮助我指出问题所在,thnx。
这是脚本:
#!/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:line 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
关于什么可能导致此错误的任何想法?
谢谢你的帮助。
答案 0 :(得分:3)
替换
if [[ $? !=0 ]];
与
if [[ $? != 0 ]];
缺少的空间导致了这个问题。
答案 1 :(得分:0)
在bash中,“-ne”应该用于测试整数相等性。以第53行为例,看起来是对的。
在命令行上使用“man test”查看批处理脚本中条件测试的所有选项。对于整数比较,它说:
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
我的助记符是:对于数字比较,使用字符串测试运算符(ne,eq,gt)进行字符串比较,使用您在其他语言中使用的数字(“=”和“!'”)。基本上它与你(我)所期望的相反。