Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 [ $? !=0 ]; then echo "Poblems Creating Backup File;" scp $BACKUPDIR/$BACKUPFILE $BACKUPHOST: if [ $? !=0 ]; then echo "Problems Copying Backup File to Backup Host" fi }
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
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 [[ $? !=0 ]]; 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
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
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
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.
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 }
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.
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 01:59 PM.
Reason: Make the example more proper, goarilla could you please post ones more professional in the future?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.