bash switch-case语句给出了语法错误

时间:2016-01-21 07:34:32

标签: bash switch-statement

#!/bin/bash

echo "Title: " read title echo "" until [ -n "$title" ] do echo "Please enter title: " read title echo "" done echo "Author: " read author echo "" until [ -n "$author" ] do echo "Please enter author: " read author echo "" done

check=cat ./BookDB.txt | egrep -ie "\b""$title""\b" | egrep -ie "\b""$author""\b" if [ -z "$check" ] then echo "Error! Book does not exist!" #need some code to continue else echo "Book found!" all=cat ./BookDB.txt | grep -i "$title"

oldtitle=`echo "$all" | cut -d ":" -f1`
oldauthor=`echo "$all" | cut -d ":" -f2`
oldprice=`echo "$all" | cut -d ":" -f3`
oldavail=`echo "$all" | cut -d ":" -f4`
oldsold=`echo "$all" | cut -d ":" -f5`

fi while : do echo "" echo " a) Update title" echo " b) Update author" echo " c) Update price" echo " d) Update quantity available" echo " e) Update quantity sold" echo " f) Back to main menu" echo "" echo -n "Please enter your choice: " read option

case $option in a ) echo -n "New title: " read newtitle if [ "$oldtitle" = "$newtitle" ] then echo "Title is the same as original" else all_title=`cut -f 1 -d ":" ./BookDB.txt` check=`echo "$all_title" | grep -i "\b""$newtitle""\b"` fi if [ -n "$check" ] then echo "Book title already exists." else sed -i "s/$oldtitle:/$newtitle:/g" ./BookDB.txt echo "Book title successfully updated." fi b ) esac done

我无法在bash上运行此代码。他们说我的CASE选择在这一行有一个语法错误 b)

我认为没有任何问题

1 个答案:

答案 0 :(得分:1)

使用case语句时,需要使用;;

结束每个子句
case $option in 
    a )
        # do something
        ;;
    b )
        # do something
        ;;
esac

您可以阅读here了解有关案例陈述的更多详情。