LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-16-2013, 10:54 PM   #1
Chris1969
LQ Newbie
 
Registered: Jun 2013
Posts: 6

Rep: Reputation: Disabled
syntax error near unexpected token `}'


Hello Guys

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 57: syntax error near unexpected token `}'
backupscripts.sh: line 57: `}'

Hope you can help me out, thnx.

Here is the script:

PHP Code:
#!/bin/bash

# Author: Chris Navarrete
# 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
=SERVER IP FOR REMOTE BACKUP
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 [ $? !=]; then echo "Poblems Creating Backup File;"
     
scp $BACKUPDIR/$BACKUPFILE $BACKUPHOST:
     if [ $? !=
]; then echo "Problems Copying Backup File to Backup Host"
fi
}

checkbackupdir
backup

#END 
 
Old 06-16-2013, 11:25 PM   #2
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
What is line 57 in your script?...also you use of ' ' for functions is antiquated, its best to use $()... Example..

BACKUPFILE=/backup.$(date +%F).bz2

Fix the rest throughout the script.

Last edited by cbtshare; 06-16-2013 at 11:31 PM.
 
Old 06-17-2013, 12:22 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Also
Code:
   if [ $? !=0 ]; then echo "Poblems Creating Backup File;" 

# should have spaces around operator, needs a 'fi'.
# uses wrong operator,  [[ ]] better
   if [[ $? -ne 0 ]]; then echo "Poblems Creating Backup File;"  fi
similarly for next 'if...'
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

Re [[ ]], for numeric comparisons, consider (( ))
Code:
if (( x == 3 ))
then
	echo "x is 3"
else
	echo "no"
fi
 
Old 06-17-2013, 06:03 PM   #4
Chris1969
LQ Newbie
 
Registered: Jun 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hello

Thank you for your quick replies, I have made the changes sugested, now when I run the script I get this error:

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'

This is what shows on line 55 in the Script:

PHP Code:
if [[ $? !=]]; then echo "Problems Copying Backup File to Backup Host;" fi 
This is how the Script looks now after the changes made to it:

PHP Code:

#!/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



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 [[ $? !=
]]; then echo "Problems Copying Backup File to Backup Host;" fi

}

checkbackupdir
backup

#END 
Would really apriciate any help, thnx.
 
Old 06-17-2013, 07:07 PM   #5
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
if [[ $? !=0 ]];

Should have a space

If [[ $? != 0 ]]
 
Old 06-17-2013, 07:12 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Yeah, and as I said, that's the wrong operator anyway; read those links I put above. It should be -ne or use (( )).
 
Old 06-17-2013, 07:18 PM   #7
Chris1969
LQ Newbie
 
Registered: Jun 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
cbtshare thank you for the quick reply, I made the modification you sugested now line 55 looks like this:

PHP Code:
if [[ $? != ]]; then echo "Problems Copying Backup File to Backup Host;" fi 
After running the Script again this is the error I get:

backupscripts.sh: line 57: syntax error near unexpected token `}'
backupscripts.sh: line 57: `}'

In line 57 this is what I have:

PHP Code:

Thnx for the help, any ideas ?
 
Old 06-17-2013, 07:37 PM   #8
Chris1969
LQ Newbie
 
Registered: Jun 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
chrism01 thank you for the reply, I have corrected the operator from != to -ne

PHP Code:
if [[ $? -ne 0 ]]; then echo "Problems Copying Backup File to Backup Host;" fi 
Runned the script again and this is the error I got:

backupscripts.sh: line 57: syntax error near unexpected token `}'
backupscripts.sh: line 57: `}'

This is what it displays in line 57:

PHP Code:

This is how the script looks now:

PHP Code:
#!/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



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 [[ $? -
ne 0 ]]; then echo "Problems Copying Backup File to Backup Host;" fi

}

checkbackupdir
backup

#END 
Thnx.
 
Old 06-17-2013, 08:56 PM   #9
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 6,872

Rep: Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249
Just a guess due to my limited knowledge of bash but, I would put another "fi" just above the last "}" since you have three if statements in the backup function.
 
Old 06-17-2013, 09:21 PM   #10
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
Correct..you need another 'fi' to close the main 'if' of the function backup.

fi
}
Checkbackupdir
Backup

#end

Last edited by cbtshare; 06-17-2013 at 09:23 PM.
 
Old 06-17-2013, 09:47 PM   #11
Chris1969
LQ Newbie
 
Registered: Jun 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
I have applied all sugested changes to the Script, now when I run the Script this is the error I get:

PHP Code:
backupscripts.shline 34syntax error near unexpected token `('

backupscripts.sh: line 34: 
`foo checkbackupdir () {
This is line 34:

PHP Code:
foo checkbackupdir () { 
This is how the Script looks now with all changes applied to it:

PHP Code:
#!/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 [ ! -
"$BACKUPDIR]
then

    
echo "Creating Backup Directory because it doesn\'t exist !"
    
mkdir ~/backup
    COUNT
=0
#    exit 0
else
    
COUNT=$(ls "$BACKUPDIRwc -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 sugestions ? Thnx.
 
Old 06-17-2013, 10:00 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
WTF is 'foo' doing there ??
You should really read those links I gave.
Try this http://www.cyberciti.biz/faq/bash-sh...tion-examples/

FYI, if you add 'set -xv' near the top, it'll show you what the parser is doing
Code:
#!/bin/bash  # good idea to specify this to ensure you get the shell you want
set -xv
 
Old 06-17-2013, 10:07 PM   #13
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
I guess this is home work and you found a script online an modified it to do what you wanted....just read and learn the basics along with that too.
 
Old 06-17-2013, 10:25 PM   #14
Chris1969
LQ Newbie
 
Registered: Jun 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
cbtshare your last comment is not really constructive, this is not homework and this is not a web script, any ways if you have nothing else to say, please dont come here to increase your posts amount.
 
Old 06-18-2013, 07:12 AM   #15
goarilla
LQ Newbie
 
Registered: Sep 2005
Location: Belgium
Distribution: slackware 10
Posts: 13

Rep: Reputation: 2
Foo is not a keyword, remove it. Either call your function foo or checkbackupdir but spaces are not allowed
in function names.

Some small notes

-ne
-le
-gt
-ge
-eq

are binary arithmetic operators
meaning they need two operators and they need to be integers eg:

$? -eq 0 # valid comparison: both operands are ints
# True: success of previous command, False: error of previous command
$? -eq "lol" # COMPARISON ERROR: operand2 is string (lol) !!!


=
!=

are binary string operators

PIZZA="LARGE" # assignment of PIZZA
"$PIZZA" = "LARGE" # valid comparison: both operands are strings and True
# since the test is successful
"$PIZZA" = 12 # COMPARISON ERROR: operand2 is int (12)

I hope this is clear.

Last edited by Mara; 06-18-2013 at 02:59 PM. Reason: Make the example more proper, goarilla could you please post ones more professional in the future?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Me: Please syntax error near unexpected token `|' pathrosa Linux - Newbie 4 11-08-2012 06:18 PM
syntax error near unexpected token `else' dnaqvi Linux - General 5 12-08-2010 04:37 AM
Syntax error near unexpected token fi Nortekman Linux - Newbie 8 02-27-2009 04:48 PM
syntax error near unexpected token -what does this mean? rmnature Linux - Newbie 5 02-14-2009 09:19 AM
syntax error for unexpected token `(' Steve Spurr Linux - Newbie 6 09-22-2006 09:19 AM


All times are GMT -5. The time now is 09:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration