error in script (591 Views)
Reply
Advisor
mick001
Posts: 24
Registered: ‎12-08-2002
Message 1 of 12 (591 Views)
Accepted Solution

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

Honored Contributor
Sameer_Nirmal
Posts: 2,108
Registered: ‎02-21-2004
Message 2 of 12 (591 Views)

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.
Trusted Contributor
Sheriff Andy
Posts: 258
Registered: ‎03-21-2004
Message 3 of 12 (591 Views)

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.

Honored Contributor
Mel Burslan
Posts: 3,214
Registered: ‎08-26-1998
Message 4 of 12 (591 Views)

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...
Honored Contributor
Leif Halvarsson_2
Posts: 6,682
Registered: ‎01-09-2002
Message 5 of 12 (591 Views)

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
Honored Contributor
Leif Halvarsson_2
Posts: 6,682
Registered: ‎01-09-2002
Message 6 of 12 (591 Views)

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
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 7 of 12 (591 Views)

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!
Honored Contributor
Mel Burslan
Posts: 3,214
Registered: ‎08-26-1998
Message 8 of 12 (591 Views)

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...
Advisor
mick001
Posts: 24
Registered: ‎12-08-2002
Message 9 of 12 (591 Views)

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
Valued Contributor
Mark Ellzey
Posts: 242
Registered: ‎12-27-1998
Message 10 of 12 (591 Views)

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
Honored Contributor
Sameer_Nirmal
Posts: 2,108
Registered: ‎02-21-2004
Message 11 of 12 (591 Views)

Re: error in script

You are trying to have a exit status in each
of those loops ( tar and gzip). In the first tar loop, if the tar failed, you are supposed to have exit 1 and not exit 0 . Then put exit 0 after echo "tar ok" . If successful return of any status should be exit 0 becuase shell understand it that way.
If you are taking care of exit status return at each loop, you don't need to have exit at the end. Those last exit will be always the return values for any condition of any loop.
So you will get always 0 or 1 as return values irrespective of the actual return from the loops.

Honored Contributor
Mel Burslan
Posts: 3,214
Registered: ‎08-26-1998
Message 12 of 12 (591 Views)

Re: error in script

Mick,

exit 0 is not an error code let alone being the only one. Au contraire, exit 0 is the exit code for error-free completion of an executable as a convention but you can call successful completion as exit code 5 and trap for this one if your heart desires so. It is not an absolute requirement as long as you know what you are doing.

In my opinion, your script could be better of with this type of struct:

#!/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 1
else
echo "tar ok"
fi


gzip $filename
if [ $? -ne 0 ];
then echo "gzip failed"
exit 2
else
echo "gzip ok"
fi

echo "all procedure completed successfully"
exit 0

so, according to the above, if you test the exit code of the script, you can tell the user where exactly the failure was.


________________________________
UNIX because I majored in cryptology...
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.