user3114615
user3114615

Reputation: 1

./test: line 22: syntax error near unexpected token `done'

#!/bin/bash
# script to create new instance by taking inputs from user

source /root/keystonerc_admin
unset http_proxy

function main {
    echo "Please choose:
    1. Create instance from an image
    2. Create instance from a volume
    3. Exit"

    While true do
        read SELECT
        case "$SELECT" in
            1) SELECT=func_create_from_image;;
            2) SELECT=func_create_from_volume;;
            3) SELECT=exit;;
            *) echo Invalid selection.; continue
        esac
        break
    done
}

function func_create_from_image {
echo "List of flavors"
nova flavor-list
echo "Enter flavor id from the list"
read flavor_id

echo "List of images"
nova image-list
echo "Enter image id from the list"
read image_id

echo "List of security groups"
nova secgroup-list --all-tenants
echo "Enter security group name from the list"
read secgroup_name

echo "List of keypairs"
nova keypair-list
echo "Enter keypair name from the list"
read keypair_id

echo "Enter display name of instance"
read display_name

## use "set" for debugging
##set -x
nova boot --display $display_name --flavor $flavor_id --image $image_id \
--key_name $keypair_id --security_group $secgroup_name
##set +x
}

function func_create_from_volume {
echo "Please choose:
1. Use existing bootable volume
2. Create new bootable volume 
3. Exit"

while true do
    read SELECT
    case "$SELECT" in
        1) SELECT=create_instance_from__existing_volume;;

        2) SELECT=create_instance_from_new_volume;;

        3) SELECT=exit;;

        *) echo Invalid selection.; continue

        esac
        break

    done
}

create_instance_from_new_volume {

        echo "Specify image id from the below list"
        nova image-list
        read image_id

        echo "specify size in GB, only specify number"
        read vol_size

        echo "Specify image name to be displayed in cinder list"
        read vol_disp_name

        set -x
        cinder create --image-id $image_id --display-name $vol_disp_name $vol_size
        set +x

        echo "volume created"
        cinder list

        echo "create instance"
        create_instance_from__existing_volume
}

create_instance_from_existing_volume {

        echo "Specify volume id from the below list"
        cinder list
        read vol_id

            echo "Pick flavor id from the below list"
        nova flavor-list
        read flavor_id

        echo "specify size in GB, only specify number or leave blank for system to decide"
        read vol_size

        echo "Specify type of volume either snap or other or leave blank if not known"
        read vol_type

        echo "Specify 1 if volume should be deleted when the instance terminates or specify 2 if volume remains after 

        instance terminates" 
        read vol_del
            if [$vol_del!=0 or $vol_del!=1] then
            echo "Specify either 1 or 0"
            fi

        echo "Specify display name for instance"
        read inst_disp_name

        echo "Creating instance"
        nova boot --flavor $flavor_id --block_device_mapping vda=$vol_id:$vol_type:$vol_size:$vol_del $inst_disp_name

}

Upvotes: 0

Views: 1121

Answers (3)

David W.
David W.

Reputation: 107080

In theory, do should be on a separate line:

while ....
do
    ....
done

Same with then:

if .....
then
    .....
fi

However, people like putting them on the same line in the K&R Style. To do that, you need a semicolon to show the shell when the while line ends, so the do is on a different line:

while ....; do
    ....
done

This may no longer be necessary in newer versions of BASH, but it should be done. I prefer using an extra line just because the shell prefers it.

The other problem and the actual problem has to do with the While on line #13 as While (uppercase W and not lowercase w).

Two thing will help catch this in the future:

  • Use set -xv to turn on debugging and set +xv to turn off debugging. In this case, you'll see the error happens when you enter the while loop when Line #13 gets executed. You can set export PS4=\$LINENO+ to show line numbers when this debugging is on. To turn off debugging, you set +xv.
  • Use a program editor that has syntax highlighting. I saw this immediately because the While wasn't in the correct color. I took a closer look and saw the capitalized While.

Upvotes: 1

karolba
karolba

Reputation: 1029

Use

while true; do

instead of

While true do

because do isn't an argument to true command

Upvotes: 1

Ian Roberts
Ian Roberts

Reputation: 122404

While true do

Bash is case-sensitive - you need while rather than While. And you also need to either move the do to the next line or put a semicolon before it

while true; do

Also your if condition further down is suspect:

if [$vol_del!=0 or $vol_del!=1] then

Aside from the syntax errors, this condition will always be true (since if the value is 1 then it is not 0 and vice versa). I suspect you wanted and rather than or:

if [ $vol_del != 0 -a $vol_del != 1 ]; then

Upvotes: 1

Related Questions