2

The brig start n command should create a bucket brigade of n passer processes in the passer script but when I run this script to begin the process I am getting the following error:

./brig: line 121: syntax error near unexpected token `else'
./brig: line 121: `else'

Below is the script: Thanks for any help you can give!:

#!/bin/bash

#bucket brigade - brigade script
#the user defines a fire
#the user defines number of passers
#then passers extinguish the fire

if [[ "$2" = *[!0-9]* ]];
then
echo "The second argument may only contain digits"
exit
fi
#$2 must be empty or contain only digits
#to pass this point

case "$1" in

fire)
if [ -z "$2" ];
then
echo Please provide a fire size
echo 'exe: brig fire 10'
else
echo "$2" > .fire.size
echo "A fire of $2 intensity has started"
fi
;;

alarm)

rm .report.pas.* 2> /dev/null

if [ -z "$2" ];
then
echo Please provide the number of passers
echo 'exe: brig alarm 5'

elif [ "$2" -lt 2 ];
then
echo "There needs to be more than one passer"

else

passvar="$2"
export totalpass="$2"
#passers check exported variable to see if they
#are high number

touch .keep.passing

echo "$totalpass Passers have been activated"
while [ "$passvar" -gt 0 ];
do

bash passer "$passvar" &
((passvar--))
done
fi
;;

status)

firenow=0

clear
echo "Report of situation as of"
echo $(date)

if  [ -e .fire.size ];
then
firenow=$(cat .fire.size)
fi

if [ "$firenow" -gt 0 ];
then
echo "The fire is blazing with $firenow intensity"
else
echo "No fire is active"
fi

if [ -e .keep.passing ];
then

touch .passers.pause
sleep 3

ls | grep bucket. > .current.buckets
cat bucket.* > .bucket.contents

echo "The current active buckets and contents:"
paste .current.buckets .bucket.contents | tr '\t' '\n'

rm .current.buckets
rm .bucket.contents
rm .passers.pause

else

echo "There are no buckets currently active"

fi

;;

quit)
rm .keep.passing 2> /dev/null
sleep 1
rm .fire.size 2> /dev/null
rm bucket.* 2> /dev/null

if [ -e .report.pas.1 ];
then
cat .report.pas.* >> .unified.report
fi

clear
echo 'Generating Report for this session'
sleep 3
cat .unified.report | more
else
echo "No reports exist at this time"
fi
;;

*)
echo 'Usage: brig [fire (n)|alarm (n)|status|report|quit]'
esac
3

There is a fi that should not exist in the line 114. The correct code would be this:

#!/bin/bash

# Bucket brigade - brigade script
# The user defines a fire
# The user defines number of passers
# Then passers extinguish the fire

if [[ "$2" = *[!0-9]* ]]; then
    echo "The second argument may only contain digits"
    exit
fi

# $2 must be empty or contain only digits to pass this point

case "$1" in

    fire)
        if [ -z "$2" ]; then
            echo Please provide a fire size
            echo 'exe: brig fire 10'
        else
            echo "$2" > .fire.size
            echo "A fire of $2 intensity has started"
        fi
    ;;

    alarm)

        rm .report.pas.* 2> /dev/null

        if [ -z "$2" ]; then
            echo Please provide the number of passers
            echo 'exe: brig alarm 5'
        elif [ "$2" -lt 2 ]; then
            echo "There needs to be more than one passer"  
        else

            passvar="$2"
            export totalpass="$2"
            #passers check exported variable to see if they are high number
            touch .keep.passing
            echo "$totalpass Passers have been activated"
            while [ "$passvar" -gt 0 ]; do

                bash passer "$passvar" &
                ((passvar--))
            done
        fi
    ;;

    status)

        firenow=0

        clear
        echo "Report of situation as of"
        echo $(date)

        if  [ -e .fire.size ]; then
            firenow=$(cat .fire.size)
        fi

        if [ "$firenow" -gt 0 ]; then
            echo "The fire is blazing with $firenow intensity"
        else
            echo "No fire is active"
        fi

        if [ -e .keep.passing ]; then

            touch .passers.pause
            sleep 3

            ls | grep bucket. > .current.buckets
            cat bucket.* > .bucket.contents

            echo "The current active buckets and contents:"
            paste .current.buckets .bucket.contents | tr '\t' '\n'

            rm .current.buckets
            rm .bucket.contents
            rm .passers.pause

        else
            echo "There are no buckets currently active"
        fi

    ;;

    quit)
        rm .keep.passing 2> /dev/null
        sleep 1
        rm .fire.size 2> /dev/null
        rm bucket.* 2> /dev/null

        if [ -e .report.pas.1 ]; then
            cat .report.pas.* >> .unified.report
        # This is the wrong "fi" ######################################## ;-)

            clear
            echo 'Generating Report for this session'
            sleep 3
            cat .unified.report | more
        else
            echo "No reports exist at this time"
        fi
    ;;

    *)
        echo 'Usage: brig [fire (n)|alarm (n)|status|report|quit]'
esac

Note that I've indented your code, Personally, I can give you a few recommendations:

  • Please, by the God sake! do not put semicolons and then newlines! ;-) If do you want to make a good conditional take one of these examples:

    1. then in the same line:

      if [ "my coding style" == "can be improved" ]; then
          echo "I accept suggestions ;-)"
      fi
      
    2. then in a separated line:

      if [ "my coding style" == "can be improved" ]
      then
          echo "I accept suggestions ;-)"
      fi
      
  • I don't know why you're splitting short comments on various lines, I don't do that.

  • Please, indent your code 2, 4 or 8 spaces (I'd recommend 4).
  • Don't sleep 3 seconds while you're doing nothing.

Sorry if I made any mistake, English isn't my mother tongue.

Hope it helps.

  • You're welcome! If you don't want any answer more please accept mine to close the question. If do you have any doubt more, don't hesitate to ask on this site! – 0x2b3bfa0 Apr 8 '15 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.