Where's the error

Discuss Applications
Post Reply
User avatar
Basher52
guru
guru
Posts: 919
Joined: Wed Oct 22, 2003 5:57 am
Location: .SE

Where's the error

Post by Basher52 » Wed Jun 22, 2011 12:17 am

Code: Select all

#!/bin/bash

for subnet in $(/sbin/ifconfig | awk '/inet addr:/ && ! /127.0.0.1/{sub(/inet addr:/,""); sub(/\.[0-9]+\.[0-9]+$/,"",$1); print $1}')
 do
  if [ grep -qw $subnet ./subnet.adm ] ; then
        parted /dev/sda rm 3
        parted /dev/sda rm 2
        parted /dev/sda rm 1

        if [ `zisview ComputerName|grep -c BK` != 0 ]; then
                zisedit -c
        fi

        img -restore -proxy bas-mdt.zmg

        if [ test -e /proc/acpi/battery/*/state ] ; then
                img -restore -proxy adm-laptop.zmg ; /bin/eject /dev/cdrom ; reboot -f
        else
                img -restore -proxy adm-desktop.zmg ; /bin/eject /dev/cdrom ; reboot -f
  elif [ grep -qw $subnet ./subnet.utb ] ; then
        parted /dev/sda rm 3
        parted /dev/sda rm 2
        parted /dev/sda rm 1

        if [ `zisview ComputerName|grep -c BK` != 0 ]; then
                zisedit -c
        fi

        img -restore -proxy bas-mdt.zmg

        if [ test -e /proc/acpi/battery/*/state ] ; then
                img -restore -proxy utb-laptop.zmg ; /bin/eject /dev/cdrom ; reboot -f
        else
                img -restore -proxy utb-desktop.zmg ; /bin/eject /dev/cdrom ; reboot -f
  else echo Unknown subnet: $subnet. Script canceled and eagerly awaits instructions...
 fi
done
with this I get this error:

Code: Select all

bin/ftl: line 20: syntax error near unexpected token 'elif'
bin/ftl: line 20: '  elif [ grep -qw $subnet ./subnet.utb ] ; then'
In this last one I added the brackets but still get the same result.
Can anyone see where it is, cos I can't

'zisview', 'zisedit' and 'img' you can pass cos they do work and are part of the OS and that is on the CD (originally Suse I think)

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Re: Where's the error

Post by Void Main » Wed Jun 22, 2011 12:28 am

Your if structures have to be like this:

Code: Select all

if xxx; then
   blah blah;
fi
or

Code: Select all

if xxx; then
   blah blah;
else
   other other;
fi
or

Code: Select all

if xxx; then
    blah blah;
elif yyy; then
    ugh ugh;
else
    other other;
fi
In one spot you incorrectly have:

Code: Select all

if xxx; then
   blah blah;
else
   other other;
elif yyy; then
   ugh ugh;
fi
If you are going to use an "else" in your if structure it has to be at the end of it.

User avatar
Basher52
guru
guru
Posts: 919
Joined: Wed Oct 22, 2003 5:57 am
Location: .SE

Re: Where's the error

Post by Basher52 » Wed Jun 22, 2011 1:26 am

hmm, so you can't have an 'if' inside another as I do here?
I'll color the "if's" etc so you can see what I'm thinking of.
Same color means that they are related.
(Seems I can't use the 'code' tag with colors :P )


#!/bin/bash

for subnet in $(/sbin/ifconfig | awk '/inet addr:/ && ! /127.0.0.1/{sub(/inet addr:/,""); sub(/\.[0-9]+\.[0-9]+$/,"",$1); print $1}')
do
if [ grep -qw $subnet ./subnet.adm ] ; then
parted /dev/sda rm 3
parted /dev/sda rm 2
parted /dev/sda rm 1

if [ `zisview ComputerName|grep -c BK` != 0 ]; then
zisedit -c
fi
img -restore -proxy bas-mdt.zmg

if [ test -e /proc/acpi/battery/*/state ] ; then
img -restore -proxy adm-laptop.zmg ; /bin/eject /dev/cdrom ; reboot -f
else
img -restore -proxy adm-desktop.zmg ; /bin/eject /dev/cdrom ; reboot -f
elif [ grep -qw $subnet ./subnet.utb ] ; then
parted /dev/sda rm 3
parted /dev/sda rm 2
parted /dev/sda rm 1

if [ `zisview ComputerName|grep -c BK` != 0 ]; then
zisedit -c
fi
img -restore -proxy bas-mdt.zmg

if [ test -e /proc/acpi/battery/*/state ] ; then
img -restore -proxy utb-laptop.zmg ; /bin/eject /dev/cdrom ; reboot -f
else
img -restore -proxy utb-desktop.zmg ; /bin/eject /dev/cdrom ; reboot -f
else echo Unknown subnet: $subnet. Script canceled and eagerly awaits instructions...
fi
done

User avatar
Basher52
guru
guru
Posts: 919
Joined: Wed Oct 22, 2003 5:57 am
Location: .SE

Re: Where's the error

Post by Basher52 » Wed Jun 22, 2011 1:28 am

DOH!!!
Just found it, I forgot to 'fi' the 'if' statement that has the 'else'.

I'm a bit confused with all the examples I've seen when to use 'fi'
Sometimes they use it and sometimes they don't, but in this case I need to use it to "quit" the 'if' before the continuation of the previous one.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Re: Where's the error

Post by Void Main » Wed Jun 22, 2011 2:44 am

All examples should have a 'fi' or they are bad examples that will not work. If you properly indent you won't run into this sort of problem. For example:

Code: Select all


if xxx; then

    if yyy; then
       echo "yyy";
    else
       echo "not yyy";
    fi

elif zzz; then

   echo "zzz";

else

   echo "something else entirely";

fi
Every if MUST have a fi.

User avatar
Basher52
guru
guru
Posts: 919
Joined: Wed Oct 22, 2003 5:57 am
Location: .SE

Re: Where's the error

Post by Basher52 » Wed Jun 22, 2011 4:18 am

k, got it :)

Post Reply