LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   syntax error near unexpected token `fi' (https://www.linuxquestions.org/questions/programming-9/syntax-error-near-unexpected-token-%60fi-218062/)

Warmduvet 08-15-2004 08:13 PM

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

leinhos 08-15-2004 10:56 PM

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.

Warmduvet 08-15-2004 11:49 PM

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

leinhos 08-16-2004 12:00 AM

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
....
....
#

Dark_Helmet 08-16-2004 12:26 AM

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.

leinhos 08-16-2004 12:31 AM

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...

DanTheKiwi 08-16-2004 12:32 AM

I just ran it on my FreeBSD box and it works fine... The last line is the "Dagnabit, the backup failed. Time to debug."

Dark_Helmet 08-16-2004 12:46 AM

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 ;)

Warmduvet 08-16-2004 01:20 AM

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.

jlliagre 08-16-2004 01:59 AM

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"

Warmduvet 08-16-2004 02:09 AM

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

leinhos 08-16-2004 02:47 AM

I think O'reilly has a few books on assorted shells. Their books are always excellent, in my opinion.

apeekaboo 08-16-2004 02:49 AM

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.

dannyp 08-16-2004 04:04 AM

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.

JLM-TOC 08-16-2004 10:48 AM

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


All times are GMT -5. The time now is 02:50 AM.