Register
It is currently Sun Dec 21, 2014 3:32 am

Function Syntax


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon Feb 21, 2011 3:24 am   

Joined: Mon Feb 21, 2011 3:14 am
Posts: 2
Hi,

I have been using a bash script to backup a MySQL database for at least a year now, however, I have recently upgraded my Debian distribution to Squeeze and my script will no longer run. It complains about a problem with a function.

Any ideas what is going wrong here?

This is the original script:
Code:
#!/bin/sh -x
#
#Simple script to backup the expenses database
#to a bzip2 compressed file.
#
#use bzip2 -d to decompress.
#
#00 22 * * * /root/scripts/expenses_backup.sh
#

function report () {
  echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> $LOG
}

BDIR=/var/local/backup/expenses
DOW=`date +%a`
LOG=/root/scripts/logs/expenses_backup.log

export PATH="$PATH:/usr/bin:/bin"

report "Starting backup of expenses database"

mysqldump -uexpenses -pexpenses expenses | bzip2 > $BDIR/$DOW.sql.bz2

report "Completed backup of expenses database"



When executed it complains about:

./expenses_backup.sh
./expenses_backup.sh: 11: Syntax error: "(" unexpected

To test this I created a smaller simpler script, but I can only get this to run when I remove the word function. I like to have to word function in there so others can understand the scripts I write.

Code:
#!/bin/sh

report () {
        echo report
}
report test


Many thanks for helping me understand where I am going wrong here. I have read man pages and been googling to no avail b-(


Top
 Profile  
 PostPosted: Mon Feb 21, 2011 6:53 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi catcher!

I wonder if it's a difference in the shell?

You're using /bin/sh, which on some systems can be linked to /bin/bash, and in some cases (Debian Squeeze is one of them, I think) it's linked to /bin/dash.

Try changing your shabang line to #!/bin/bash instead of 'sh' and see if that solves the problem. Bash should accept either syntax for 'function' (with our without the word 'function')


Top
 Profile YIM  
 PostPosted: Mon Feb 21, 2011 11:18 am   

Joined: Mon Feb 21, 2011 3:14 am
Posts: 2
Top marks to jeo!!

I'm thinking the upgrade from Debian Lenny to Squeeze change the packages slightly. /bin/sh is now linked to /bin/dash

You learn something new everyday.

Many Thanks


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP