./test:第22行:意外令牌“完成”附近的语法错误

时间:2014-01-06 12:24:21

标签: bash loops while-loop

#!/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

}

3 个答案:

答案 0 :(得分:1)

While true do

Bash区分大小写 - 您需要while而不是While。而且你还需要将do移动到下一行或在它之前放一个分号

while true; do

您的if条件进一步下降是可疑的:

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

除语法错误外,此条件始终为真(因为如果值为1则不为0,反之亦然)。我怀疑你想要而不是或者:

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

答案 1 :(得分:1)

使用

while true; do

而不是

While true do

因为do不是true命令的参数

答案 2 :(得分:1)

理论上,do应该在一个单独的行上:

while ....
do
    ....
done

then相同:

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

然而,人们喜欢将它们放在K& R Style中的同一条线上。为此,当while行结束时,您需要一个分号来显示shell,因此do位于不同的行上:

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

在较新版本的BASH中可能不再需要这样做,但应该这样做。我更喜欢使用额外的行,因为shell更喜欢它。

另一个问题和实际问题与第13行的While While(大写W而不是小写w)有关。

将来有两件事有助于解决这个问题:

  • 使用set -xv打开调试set +xv以关闭调试功能。在这种情况下,当您执行第13行时,您将看到当您进入while循环时发生错误。启用此调试时,您可以设置export PS4=\$LINENO+以显示行号。要关闭调试,请set +xv
  • 使用具有语法突出显示的程序编辑器。我立即看到了这一点,因为While不在正确的颜色中。我仔细看了一下,看到了大写While