[Bash] while & until commands

While

The while loop executes a block of code as long as a specified condition is true.

while [ condition ]; do
  commands
done

Example:

#!/bin/zsh

count=1
while [ $count -le 5 ]; do
  echo "Count: $count"
  count=$((count + 1))
done

Until

The until loop executes a block of code as long as a specified condition is false.

until [ condition ]; do
  commands
done

Example:

#!/bin/zsh

target=7
guess=0

until [ $guess -eq $target ]; do
  echo "Guess the number: "
  read guess
  if [ $guess -lt $target ]; then
    echo "Too low!"
  elif [ $guess -gt $target ]; then
    echo "Too high!"
  else
    echo "Correct!"
  fi
done
posted @   Zhentiw  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-05-21 [Debug] Debugger Statements
2019-05-21 [Svelte 3] Use an onMount lifecycle method to fetch and render data in Svelte 3
2019-05-21 [Svelte 3] Use DOM events and event modifiers in Svelte 3
2019-05-21 [React] Create a Persistent Reference to a Value Using React useRef Hook
2019-05-21 [React] Reduce Code Redundancy with Custom React Hooks
2018-05-21 [Javascript] Highlights from IO18 Javascript new features
2018-05-21 [CSS3] :empty Selector
点击右上角即可分享
微信分享提示