PDA

View Full Version : Any Linux shell programmers here??



oldfart
23-08-2009, 05:41 PM
having problems with a script - anyone help - this errors 'end of file fi expected'


if ! [ -f /media/hdd/ext.epg.dat ]; then exit 0 ; fi

if [ -f /media/hdd/ext.epg.tgz ]; then rm /media/hdd/ext.epg.tgz ; fi


tar -czf /media/hdd/ext.epg.tgz /media/hdd/ext.epg.dat

doubleudee
23-08-2009, 06:09 PM
if............
then...............
else..............
fi

doubleudee
23-08-2009, 06:10 PM
fi closes the loop

doubleudee
23-08-2009, 06:19 PM
if ! [ -f /media/hdd/ext.epg.dat ];
then exit 0
elif [ -f /media/hdd/ext.epg.tgz ];
then rm /media/hdd/ext.epg.tgz ;
fi


tar -czf /media/hdd/ext.epg.tgz /media/hdd/ext.epg.dat

-------------------------------------------------------

is the last statement to be in the nest as well?

if so:
if ! [ -f /media/hdd/ext.epg.dat ];
then exit 0
elif [ -f /media/hdd/ext.epg.tgz ];
then rm /media/hdd/ext.epg.tgz ;
else
tar -czf /media/hdd/ext.epg.tgz /media/hdd/ext.epg.dat
fi

doubleudee
23-08-2009, 06:23 PM
Ha

see what your trying to do now

if ! [ -f /media/hdd/ext.epg.dat ];
then exit 0
elif [ -f /media/hdd/ext.epg.tgz ];
then rm /media/hdd/ext.epg.tgz ;
tar -czf /media/hdd/ext.epg.tgz /media/hdd/ext.epg.dat
fi


something like that?

oldfart
23-08-2009, 09:54 PM
nope.
1. does file No. 1 exist? NO ->exit
2. does file No.2 exist -> YES ->delete it
3. create new archive.

elif/else not required here

doubleudee
24-08-2009, 06:14 AM
nope.
1. does file No. 1 exist? NO ->exit
2. does file No.2 exist -> YES ->delete it
3. create new archive.

elif/else not required here

The way you coded it was as if the third statement was to be run irrespective of whether either of the first two statements applied,

If you want to run the third statement ONLY if the second statement was true (the delete part) then you need to embed the second and third statements in an if statement:-

if ! [ -f /media/hdd/ext.epg.dat ];
then exit 0 ;
fi

if [ -f /media/hdd/ext.epg.tgz ];
then rm /media/hdd/ext.epg.tgz ;
tar -czf /media/hdd/ext.epg.tgz /media/hdd/ext.epg.dat;
fi

oldfart
24-08-2009, 03:08 PM
nooooooo.....
the tar should be outside the if statement.
it still doesn't work:-

Syntax error: end of file unexpected (expecting "fi")

problem is:- there are loads of examples on t'internet - none of them work - still get the error above.

neilD
24-08-2009, 03:25 PM
if [ -f /media/hdd/ext.epg.tgz ]
then
rm /media/hdd/ext.epg.tgz
fi
tar -czf /media/hdd/ext.epg.tgz /media/hdd/ext.epg.dat

works for me

AJR
24-08-2009, 03:26 PM
Suggestion..

Split the code up until it works


if ! [ -f /media/hdd/ext.epg.dat ];
then exit 0 ;
fi

Did that work on its own?


if [ -f /media/hdd/ext.epg.tgz ];
then rm /media/hdd/ext.epg.tgz ;
tar -czf /media/hdd/ext.epg.tgz /media/hdd/ext.epg.dat;
fi

Did that work on its own..

If both work ok on their own, then I myself would use the "elif " to join the 2 sections together ( with just one fi at the end).

Dont forget that it may even be your editor causing the grief ... so try another.

sonic1
24-08-2009, 03:43 PM
Dont forget that it may even be your editor causing the grief ... so try another.

That's a good point AJR, so many people use DCC to edit files which is not the best tool.

I only ever use crimson editor now.

oldfart
24-08-2009, 03:50 PM
tried:-

#!/bin/sh
if ! [-f /media/hdd/ext.epg.dat];
then exit 0;
fi
echo 'here'
got the error:-
epg_aq.sh: 5: Syntax error: end of file unexpected (expecting "fi")

AJR
24-08-2009, 04:00 PM
Hi oldfart what editor are you using ..... vi perchance ?

You could try adding an EOF (very important.. no spaces before EOF)

oldfart
24-08-2009, 04:06 PM
Actually .. not thought about that .. using EditPad Lite .. will try another ...

neilD
24-08-2009, 04:09 PM
#!/bin/sh
if ! [ -f /media/hdd/ext.epg.dat ];
then exit 0;
fi
echo 'here'

Works with me, you need to make sure that you have the spaces ! [ -f and also at the end i.e. dat ];

AJR
24-08-2009, 04:11 PM
Ok... I have to go out for a while.. so good luck :D

For very simple stuff you can use vi (cos its built in..... but I do hate it)

neilD
24-08-2009, 04:14 PM
You could (if azbox has it) the od -a / od -x command to dump the contents of the file
to see if there are any dubious characters in the file, see below

od -a 1.sh
0000000 # ! / b i n / s h nl i f sp ! sp [
0000020 sp - f sp / m e d i a / h d d / e
0000040 x t . e p g . d a t sp ] ; nl t h
0000060 e n sp e c h o sp sp " N e i l sp "
0000100 ; nl f i nl e c h o sp ' h e r e '
0000120 nl
0000121

oldfart
24-08-2009, 04:37 PM
looks OK to me:-

root@dm8000:/usr/script# od -a epg_aq.sh
0000000 # ! / b i n / s h cr lf i f !
0000020 [ - f / m e d i a / h d d / e
0000040 x t . e p g . d a t ] ; cr lf t h
0000060 e n e x i t 0 ; cr lf f i cr lf
0000100 e c h o ' h e r e ' cr lf
0000115

something is going on ...
if I run the shell, I get an error:-
-sh: epg_aq.sh: not found

If I remove the first line (#!/bin/sh) it runs, but with the error:
Syntax error: end of file unexpected (expecting "fi")

wierd?

oldfart
24-08-2009, 05:03 PM
AHHHHAAA

The lights go on!!!

UNIX only wants a LF at the end of line, DOS etc. has CR LF

changed the editor and the file format - its all started to work.

AJR
24-08-2009, 06:11 PM
changed the editor and the file format

Well done oldfart

Never to late for an old dog to learn new tricks :D

Now you have sorted the issue..... you can rewrite it nicely :p

neilD
24-08-2009, 09:14 PM
I am probably the only person on the planet that like and uses vi:respect-059:

oldfart
24-08-2009, 09:50 PM
I am probably the only person on the planet that like and uses vi:respect-059:

That is SOoooo sad :respect-010:

oldfart
24-08-2009, 09:51 PM
Well done oldfart

Never to late for an old dog to learn new tricks :D

Now you have sorted the issue..... you can rewrite it nicely :p

Yep sorted - now have an email system that will send a file attachment - its only a script, but it works!!
Been programming for 28 (ish) years - so wasn't going to be beaten.

AJR
25-08-2009, 12:03 AM
28 (ish) years

Hmmmmmm...

Z80 and CPM+

Thats where I started :D

oldfart
25-08-2009, 09:43 AM
Z80, 6502, 6800

neilD
25-08-2009, 09:56 AM
Z80 with hex keypad for input, then moved on to 8086, then to 680x0 with VAX 11/780