I am having a problem with a bash script that I am working on, I am practically new to Bash Scripting and been working on this problem for hours and cant figure out what the problem could be.
When I run the script this are the errors I get:
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'
Hope you can help me out pointing me out what the problem could be, thnx.
Here is the script:
#!/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
I have made changes suggested and now when I run the script I get this error:
backupscripts.sh: line 34: syntax error near unexpected token ('
backupscripts.sh: line 34:foo checkbackupdir () {'
This is line 34
foo checkbackupdir () {
And here is the full Script with changes done to it:
#!/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
Any ideas on what could be still causing this error ?
Thank you for the help guys.
COUNT=$(ls $BACKUPDIR | wc -l)is a bad idea. See mywiki.wooledge.org/BashFAQ/004 for an explanation of why, and the correct approach.[ ! -e "$BACKUPDIR" ], not[ ! -e $BACKUPDIR ].functionkeyword when declaring functions. It's justfoo() {, notfunction foo() {-- otherwise, you're sacrificing POSIX compatibility (thefunctionkeyword is non-POSIX) and getting absolutely no benefit whatsoever.