在if和case语句中使用Bash编程循环

时间:2014-08-01 19:53:18

标签: bash loops if-statement case

为什么“if”语句中的代码将保持循环,如果将null输入到“title”变量但是对于case语句,我的脚本会出错?

 echo -n "Title :"
      read title   
   if [ -z "$title" ]; then
 echo "Please input a title"  
   while [[ -z "$title" ]] ; do
 echo -n "Title: "
      read title
   done
   fi



read author
     case "$author" in
     *[0-9,\""\!@#$%\(\)]*) echo "Please enter a name" ;;
      while  *[1-9,\""\!@#$%\(\)]*)"$author" ]] ; do
     echo -n "author "
          read author
      esac   

2 个答案:

答案 0 :(得分:0)

该行

 while  *[1-9,\""\!@#$%\(\)]*)"$author" ]] ; do
即使没有一堆语法错误,

也不会被允许。试试这个习语:

title=
while [ -z "$title ] 
do
    read -p "What is the title? " title
done

答案 1 :(得分:0)

你有\“”而且应该只是\“。这就是语法错误

另外,你不能把一个时间作为一个案例。试试这个并添加你想要的任何案例

while [ -z $author ]; do
echo "Please enter an author: "
read author  
case "$author" in
    *[0-9,\"\!@#$%\(\)]*) echo "First case - $author"
    ;;
esac
done