1748185 Members
4414 Online
108759 Solutions
New Discussion юеВ

Re: error in script

 
SOLVED
Go to solution
mick001
Advisor

error in script

WhatтАЩs wrong with this script i get the error:


./backupcdr: line 49: syntax error: unexpected end of file


please help!!

#!/bin/bash

cd /home/test
#
filename=/home/backup.`date '+%d.%m.%Y.%k.%M.%S'`.tar
indexfile=/home/backup.`date '+%d.%m.%Y.%k.%M.%S'`.txt

ls -l /home/test >$indexfile

mkdir -p /home/backup-tijd

for file in $(ls *.test); do
mv ${file} /home/backup-tijd
done

cd /home/backup-tijd
tar cvf $filename .
if [ $? -ne 0 ];
then echo "tar failed"
exit 0

else
echo тАЬtar okтАЭ
fi


gzip $filename
if [ $? -ne 0 ];
then echo "gzip failed"
exit 1
else
echo тАЬgzip okтАЭ
if


exit 0
exit 1

11 REPLIES 11
Sameer_Nirmal
Honored Contributor

Re: error in script

In the last if-then-else loop you should put fi instead of if . Secondly you can have only one exit statement at the end of the script. I guess you are expecting exit 0 there. Remove the exit 1 from the script.
Sheriff Andy
Trusted Contributor

Re: error in script

I have to agree with Sameer.

The if needs to be a fi at the end of your script. Also to re-iterate, there is no need for exit 0, exit 1.

The exit 0 signafies that the script has finished with no errors. The exit 1 signafies that the script has exited with an error. I see in your script that you have echo "tar failed", then right after that you have exit 0. That should be an exit 1 to signify that there was an error.

Mel Burslan
Honored Contributor

Re: error in script

Even though the accidental interchange of "if" with "fi" seems to be the problem here, the characters around "tar ok" and "gzip ok", being non-printable, may cause you headaches in the long run. Fancy screen output is nice I am sure but function should be your primary concern. Just my 2 cents
________________________________
UNIX because I majored in cryptology...
Leif Halvarsson_2
Honored Contributor

Re: error in script

cd /home/backup-tijd

if [ -s $( ls ) ]
then
tar cvf $filename *
gzip $filename
exit 0
else
echo "No file created"
exit 1
fi
Leif Halvarsson_2
Honored Contributor

Re: error in script


Hi,

I would suggest some modifications (last part of the script).


cd /home/backup-tijd

if [ -s $( ls ) ]
then
tar cvf $filename *
gzip $filename
exit 0
else
echo "No file created"
exit 1
fi
Sandman!
Honored Contributor

Re: error in script

The shell should be able to detect an unmatched if and produce diagnostic output saying that "if is not matched". This seems like its a problem with your tar or gzip. Could you attach your shell script instead of pasting it.

regards!
Mel Burslan
Honored Contributor

Re: error in script

also, the script you have posted, if copied verbatim from a terminal screen, has only 37 lines. In order to get a complaint about line 49, you must have gobs of blank lines at the end and if the number of lines in this script is actually showing up as 49, which you can check by

wc -l ./backupcdr

it means that there is a mismatching if-then-else or for/while/until-do-done construct, which reinforces the incorrect if/fi.
________________________________
UNIX because I majored in cryptology...
mick001
Advisor

Re: error in script

I didn├в t see the fault thanks!!

I want to use the exit to exit the script doesn├в t it work that way? Is the exit 0 only the error output?

Thanks
Mark Ellzey
Valued Contributor

Re: error in script

Mick,

Also, double-check all your quotation and backtick marks. I've seen this error caused by having unmached quotes.

Regards,
Mark