bash switch-case 语句给出语法错误
bash switch-case statement gives syntax error
#!/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)
我觉得没问题
使用 case 语句时,每个子句需要以 ;;
:
结束
case $option in
a )
# do something
;;
b )
# do something
;;
esac
您可以阅读 here 以获取有关 case 语句的更多详细信息。
#!/bin/bashecho "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)
我觉得没问题
使用 case 语句时,每个子句需要以 ;;
:
case $option in
a )
# do something
;;
b )
# do something
;;
esac
您可以阅读 here 以获取有关 case 语句的更多详细信息。