Badal Dudy
Badal Dudy

Reputation: 1

Linux Shell-Script code is not executing?

This code is not running it shows error:- 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

Upvotes: 0

Views: 43

Answers (1)

glenn jackman
glenn jackman

Reputation: 247162

You need a ;; in your case's *) branch, and the case statement ends with the esac keyword: https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs

You might want to use the select statement, to restrict the user's responses:

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

Indent your code to aid maintainability.

Upvotes: 1

Related Questions