欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 游戏 > LInux:循环语句

LInux:循环语句

2024/10/24 4:32:27 来源:https://blog.csdn.net/mohanyelong/article/details/140855110  浏览:    关键词:LInux:循环语句

LInux:循环语句

  • if-else语句

    • if 语句语法格式

      if [ $a -gt $b ];
      then echo "a>b"
      fiif [ $a -gt $b ];
      then echo "a>b"echo "a!=b"echo "true"
      fi
      
  • if-else语句

    • if-else 语句语法格式

      if [ $a -gt $b ];
      then echo "a>b"
      elseecho "a<=b"
      fiif [ $a -gt $b ];
      then echo "a>b"echo "a!=b"echo "true"
      elseecho "a<=b"
      fiif ((a>b));
      then echo "true"echo "a>b"
      elseecho "a<=b"
      fi
      
    • if else语法格式

      if [ $a -gt $b ];
      then echo "a>b"
      elif [$a -eq $b]
      thenecho "a=b"
      elseecho "a<b"
      fi
      
  • for 循环

    • for循环一般格式为

      for m in 1 2 3 4;
      do echo "values is $m"
      donefor n in This is a dong;
      do echo $n 
      done
      
  • while 语句

    • while 循环语法格式

      int=5
      while(($int>=2));
      do echo "$int"let "int--"
      doneecho "按下CTRL+D退出"
      echo "输入你喜欢的电影"
      while read film;
      doecho "${film} is ok"
      done
      
  • 无限循环

    while True;
    doecho "ok"
    donefor ((::))
    
  • until循环

    • until 循环执行一系列命令直至条件为 true 时停止

      a=0
      until [ ! $a -lt 10 ];
      doecho $aa=$((a+1))
      done
      
  • case … esac

    echo "输入1到4之间的数字"
    read -p "数字:" num
    case $num in 1) echo '1';;2) echo '2';;3) echo '3';;4) echo '4';;
    esac   s1='iambot'
    case $s1 in'iambot') echo 'bot';;'iampm') echo 'pm';;'iampl') echo 'pl';;
    esac 
    
  • 跳出循环

    • break 命令允许跳出所有循环

      echo "welcome to Amusement park"
      echo "please inter a number between 1-4"
      while :
      doread -p "please inter your number: " numcase $num in1|2|3|4)echo "you choose 1-4 you are great";;*)echo "you choose others you are bad"break;;esac
      done
      
    • continue 命令不会跳出所有循环,仅仅跳出当前循环。

      echo "welcome to Amusement park"
      echo "please inter a number between 1-4"
      while :
      doread -p "please inter your number: " numcase $num in1|2|3|4)echo "you choose 1-4 you are great";;*)echo "you choose others you are bad"continueecho 'game over';;esac
      done
      #运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句 echo "游戏结束" 永远不会被执行。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com