1

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.

6
  • COUNT=$(ls $BACKUPDIR | wc -l) is a bad idea. See mywiki.wooledge.org/BashFAQ/004 for an explanation of why, and the correct approach. Commented Jun 17, 2013 at 22:36
  • ...also, you really need to use more quotes in this script, or else things will break badly as soon as you have filenames with spaces. [ ! -e "$BACKUPDIR" ], not [ ! -e $BACKUPDIR ]. Commented Jun 17, 2013 at 22:37
  • ...also, consider using lower-case variable names. To quote greybot from irc.freenode.org's #bash channel: By convention, environment variables (PATH, EDITOR, SHELL, ...) and internal shell variables (BASH_VERSION, RANDOM, ...) are fully capitalized. All other variable names should be lowercase. Since variable names are case-sensitive, this convention avoids accidentally overriding environmental and internal variables. Commented Jun 17, 2013 at 22:37
  • Also, don't use the function keyword when declaring functions. It's just foo() {, not function foo() { -- otherwise, you're sacrificing POSIX compatibility (the function keyword is non-POSIX) and getting absolutely no benefit whatsoever. Commented Jun 17, 2013 at 22:38
  • Hello Charles, thank you for the help, have made the suggested changes but now I get another error, have updated the post with changes. Commented Jun 18, 2013 at 1:58

2 Answers 2

3

replace

if [[ $? !=0 ]];

with

if [[ $? != 0 ]];

the missing space is causing the issue.

Sign up to request clarification or add additional context in comments.

5 Comments

Hello log0 thank you for the help have made the suggested change but still get errors, please look at the main post for the changes hope you can help, regards.
@Chris yes when Charles Duffy says not to use function foo() but foo() it does not mean replacing function by foo. foo is just an hypothetical function name.
log0 thank you for pointing that out, have changed the word foo for fctn, but still get the same error:
This is the error I get: backupscripts.sh: line 34: syntax error near unexpected token (' backupscripts.sh: line 34: fctn checkbackupdir () {' Seems that even changing the function name does not correct the issue.
@Chris it is getting difficult for me to follow you :). just remove everything before the function name (i.e. checkbackupdir here), don't put function or foo or fctn nothing, just checkbackupdir(){
0

In bash "-ne" should be used for testing integer equality. Use line 53 as an example, looks like that is right.

Use "man test" on the command line to see all the options for conditional tests in a batch script. For integer comparison, it says:

   INTEGER1 -ne INTEGER2
          INTEGER1 is not equal to INTEGER2

My mnemonic is: for number comparison use the string test operators (ne, eq, gt), for string comparisons, use what you would use for numbers in other languages ("=" and "!'"). Basically it's opposite of what you (I) expect.

3 Comments

Actually -- in bash, as opposed to sh, math context should be used for testing integer equality: (( integer1 != integer2 )). The -ne syntax inside [ ] and [[ ]] is more for backwards compatibility. You can similarly use operators such as (( foo >= bar )) (yes, that's just bare foo and bar; explicit expansions such as $foo and $bar aren't required in this syntax either). See mywiki.wooledge.org/ArithmeticExpression for more.
True, but bash doesn't differentiate between the string "502" and the number 502, no?
Hello Ireeder thank you for the suggestions, I am still getting errors have made changes to the Script and have posted the updates on the post, thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.