LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-15-2004, 08:13 PM   #1
Warmduvet
Member
 
Registered: Jun 2004
Location: New Zealand
Posts: 60

Rep: Reputation: 15
syntax error near unexpected token `fi'


I'm a newbie at scripting so was trying to adapt this script I found at:

.linuxplanet.com/linuxplanet/tutorials/5257/3/

when I try and run it I get errors "unrecognized command" which is
to do with the blank lines. I removed the blank lines and get a syntax error
scripts/backup.sh: line 37: syntax error near unexpected token `fi'
scripts/backup.sh: line 37: `fi"

#!/bin/bash
## This is the famous "sha-bang" statement,
## that tells the shell which command
## interpreter to use. See man magic
## to learn the inner workings of sha-bang.
## this script uses the tar, mkdir, and cp commands
## plus Bash's conditional statements
## smart admins write lots of comments
# test for existing backup directory
# if it does not exist, create it
# -e is the Bash way of asking "does this file exist"
# $ means variable substitution.
if [ -e /mnt/backup/1backups ]
then
echo "The backup directory exists."
else
echo "A backup directory does not exist, and will be created."
mkdir /mnt/backup/1backups
echo "A backup directory has been created"
fi
# now we define our own variables
# location of files to backup
FILES=/usr/local/rpm /etc/samba /etc/httpd
# name of compressed archive
ARCHIVENAME=backups.tgz
# location of backup directory
BACKUPDIR=/mnt/backup/
# create compressed archive, copy to backup directory
tar czf $ARCHIVENAME $FILES
cp -ap $ARCHIVENAME $BACKUPDIR
if [ -e $BACKUPDIR/$ARCHIVENAME ]
then
echo "Jolly good show, the backup worked!"
else
echo "Dagnabit, the backup failed. Time to debug."
fi
 
Old 08-15-2004, 10:56 PM   #2
leinhos
LQ Newbie
 
Registered: Aug 2003
Posts: 5

Rep: Reputation: 0
Please post your "adaptation" of the script.

The only problems I see in the script as posted is that some of the variables in the script might point to files or directories that don't exist of that you don't have permission to. Check out the vaiables

FILES
BACKUPDIR

and make sure they are valid on your system.
 
Old 08-15-2004, 11:49 PM   #3
Warmduvet
Member
 
Registered: Jun 2004
Location: New Zealand
Posts: 60

Original Poster
Rep: Reputation: 15
This is the original script, the previous post is my adjusted script.


#!/bin/bash
## This is the famous "sha-bang" statement,
## that tells the shell which command
## interpreter to use. See man magic
## to learn the inner workings of sha-bang.
## this script uses the tar, mkdir, and cp commands
## plus Bash's conditional statements
## smart admins write lots of comments

# test for existing backup directory
# if it does not exist, create it
# -e is the Bash way of asking "does this file exist"
# $ means variable substitution. HOME is a builtin
# Bash variable.
if [ -e $HOME/1backups ]
then
echo "The backup directory exists."
else
echo "A backup directory does not exist, and will be created."
mkdir $HOME/1backups
echo "A backup directory has been created"
fi

# now we define our own variables
# location of files to backup
FILES=$HOME/archive-files
# name of compressed archive
ARCHIVENAME=backups.tgz
# location of backup directory
BACKUPDIR=/archive/carla/
# create compressed archive, copy to backup directory
tar czf $ARCHIVENAME $FILES
cp -ap $ARCHIVENAME $BACKUPDIR

if [ -e $BACKUPDIR/$ARCHIVENAME ]
then
echo "Jolly good show, the backup worked!"
else
echo "Dagnabit, the backup failed. Time to debug."
fi
 
Old 08-16-2004, 12:00 AM   #4
leinhos
LQ Newbie
 
Registered: Aug 2003
Posts: 5

Rep: Reputation: 0
OK. That script looks good.

Try changing the first line in the script to
#!/bin/bash -x

which will print every command as it is executed, and hopefully you can track down the prob.

If you still need help, you might look at man bash, or else please post your output from the shell, like

# bash backup.sh
....
....
#
 
Old 08-16-2004, 12:26 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I've got a question: how did you get the script? Did you download it? Did you type it up?

The reason I ask is this. You say you received "unrecognized command" errors with the original script. That's really, really odd. One possible explanation is the difference in line endings between DOS/Windows and Linux. If you downloaded the file, and it was originally written in Windows, then that's a problem. If you typed it up in Windows and transferred it to Linux (by any of: shared partition, network transfer, or floppy disk), then that's the same problem.

If this is the case, you can easily fix the problem by using this command:
dos2unix filename

That will convert the line endings from Windows to Linux.
 
Old 08-16-2004, 12:31 AM   #6
leinhos
LQ Newbie
 
Registered: Aug 2003
Posts: 5

Rep: Reputation: 0
That's a good point, but his script made it to line 37. I take this to mean that there is some other problem. I know it can be really frustrating to work with shell scripts...
 
Old 08-16-2004, 12:32 AM   #7
DanTheKiwi
LQ Newbie
 
Registered: Aug 2004
Location: Sydney, Australia
Distribution: FreeBSD, Fedora Core 2
Posts: 20

Rep: Reputation: 0
I just ran it on my FreeBSD box and it works fine... The last line is the "Dagnabit, the backup failed. Time to debug."
 
Old 08-16-2004, 12:46 AM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally posted by leinhos
That's a good point, but his script made it to line 37.
I was under the impression it complained about line 37 after he removed the blank lines. If not, I'll go sit in my corner and be quiet
 
Old 08-16-2004, 01:20 AM   #9
Warmduvet
Member
 
Registered: Jun 2004
Location: New Zealand
Posts: 60

Original Poster
Rep: Reputation: 15
Thanks for the replies, the dos2unix did the trick.
Script ran but complained about the samba dir ?


[root@jekyll scripts]# sh backup.sh
A backup directory does not exist, and will be created.
A backup directory has been created
backup.sh: line 24: /etc/samba: is a directory
tar: Cowardly refusing to create an empty archive
Try `tar --help' for more information.
cp: cannot stat `backups.tgz': No such file or directory
Dagnabit, the backup failed. Time to debug.
 
Old 08-16-2004, 01:59 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Although without relation to the error message, the line:
FILES=/usr/local/rpm /etc/samba /etc/httpd
should be
FILES="/usr/local/rpm /etc/samba /etc/httpd"
 
Old 08-16-2004, 02:09 AM   #11
Warmduvet
Member
 
Registered: Jun 2004
Location: New Zealand
Posts: 60

Original Poster
Rep: Reputation: 15
Thanks jilliagre that was it.

Can anyone suggest a good book or resource I
can use to learn the ins and outs of scripting.

TIA
 
Old 08-16-2004, 02:47 AM   #12
leinhos
LQ Newbie
 
Registered: Aug 2003
Posts: 5

Rep: Reputation: 0
I think O'reilly has a few books on assorted shells. Their books are always excellent, in my opinion.
 
Old 08-16-2004, 02:49 AM   #13
apeekaboo
Member
 
Registered: Apr 2003
Location: Stockholm/Sweden
Distribution: Kubuntu, Debian, Slax
Posts: 91

Rep: Reputation: 16
The book Learning the bash shell (O'Reilly, by C. Newham & B. Rosenblatt) is a good resource for everyone learning shell scripting.
I would also recommend sed & awk (O'Reilly, by D. Dougherty & A. Robbins) or a good perl book (O'Reilly again) for some more advanced scripting.

Sed, perl and awk are all very good at handling text, and that's basically what shell-scripting is all about: Running commands based on certain presumptions and taking actions accordingly. This often involves formatting of the output from commands so that the next command or function can work with it.
 
Old 08-16-2004, 04:04 AM   #14
dannyp
LQ Newbie
 
Registered: Mar 2004
Location: Philadelphia, PA
Posts: 14

Rep: Reputation: 0
Script Book

There is a book called Unix Shell programming by Stephen Kochan and Patrick Wood, it's published by Sams. It is IMO one of the best if not the best.
 
Old 08-16-2004, 10:48 AM   #15
JLM-TOC
LQ Newbie
 
Registered: Jul 2003
Posts: 2

Rep: Reputation: 0
Couple points:

1) sh, ksh, and bash don't allow unquoted spaces in shell-variable assignments.

So:
FILES=/usr/local/rpm /etc/samba /etc/httpd

will set FILES to "/usr/local/rpm" and then try to execute:
/etc/samba /etc/httpd
as a command. Old idiom from /bin/sh days. Bad then. Confusing today.

Change it to:
FILES="/usr/local/rpm /etc/samba /etc/httpd"

2) bash on linux has a hang-up with ! (exclamation point) inside double-quotes. Try using single quotes.

Try changing:
echo "Jolly good show, the backup worked!"

to:
echo 'Jolly good show, the backup worked!'

It has to do with C-shell like command history I believe.

Be good,
JLM
 
  


Reply



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
error: unexpected token `newline' CarlosV Programming 4 05-17-2011 03:47 AM
Many errors when 'make'ing (example: error: syntax error before `::' token) darkblade Linux - Software 5 03-02-2005 03:00 PM
got a syntax error which shows unexpected end of line when tried to run a shell scrip racer_mec Linux - Newbie 1 01-10-2005 01:43 AM
C++ syntax error before :: token HELP, i cant find the syntax error :( qwijibow Programming 2 12-14-2004 06:09 PM
Undecodable token error ?? Thymox Linux - General 3 11-18-2002 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:54 AM.

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
Open Source Consulting | Domain Registration