【问题标题】:Bash programming looping in if and case statementbash 编程循环在 if 和 case 语句中
【发布时间】:2014-08-01 19:53:17
【问题描述】:

为什么如果“title”变量输入空值,“if”语句上的代码会一直循环,但是对于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   

【问题讨论】:

    标签: bash loops if-statement case


    【解决方案1】:

    线

     while  *[1-9,\""\!@#$%\(\)]*)"$author" ]] ; do
    

    即使其中没​​有一堆语法错误,也不允许出现在那里。试试这个成语:

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

    【讨论】:

      【解决方案2】:

      你有 \"" 并且应该只是 \" 。这是语法错误

      此外,您不能将 While 作为案例。试试这个并添加你想要的任何案例

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        • 2014-01-15
        • 2017-01-15
        • 2017-03-06
        • 1970-01-01
        相关资源
        最近更新 更多