Linux Shell脚本代码没有执行?

时间:2015-07-13 12:08:11

标签: shell

此代码未运行,显示错误: - whl_case_lop1: 25: whl_case_lop1: Syntax error: "elif" unexpected (expecting ";;")

#if [ -z $1 ]
#then
#rental=****unknown item****
#elif [ -n $1 ]
#then 
#rental=$1
#else [ -n $2 ]
#then
#rent=$2
#fi
echo "1. Vehicle_on_rent"
echo "2. Living_house"
echo -n "choose option [1 or 2]? "
read cate; 
if [ $cate -eq 1 ]; 
then
echo "Enter your vehicle type"
read rental 
case $rental in 
"car") echo "for $rental in 20/km";;
"van") echo "for $rental in 15/km";;
"jeep") echo "for $rental in 10/km";;
"bike") echo "for $rental in 5/km";;
*) echo "We can't find $rental for your vehicle"
elif  [ $cate -eq 2 ]; 
then 
echo "Enter your Room requirement"
read rent
case $rent in
"1BHK") echo "for $rent is 10k";;
"2BHK") echo "for $rent is 15k";;
"3BHK") echo "for $rent is 20k";;
*) echo "we can't find $rent for your Requirement"
else
echo "Please check your requirements! Maybe you choose a wrong option"
fi

1 个答案:

答案 0 :(得分:1)

您的案例;;分支中需要*)case语句以esac关键字结尾:https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs

您可能希望使用select语句来限制用户的响应:

PS3="Enter your vehicle type: "
select rental in car van jeep bike; do
    case $rental in 
        car)  echo "for $rental in 20/km"; break ;;
        van)  echo "for $rental in 15/km"; break ;;
        jeep) echo "for $rental in 10/km"; break ;;
        bike) echo "for $rental in 5/km";  break ;;
        *)    echo "We can't find $rental for your vehicle" ;;
    esac
done

缩进代码以帮助维护。