shell中的while循环实例

1.利用while循环计算1到100的和:

示例代码1:

#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
  let sum=sum+$i
  let i++
done

echo $sum


示例代码2:利用while循环计算1到100之间所有奇数之和

#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
  let sum=sum+$i
  let i+=2
done

echo $sum


示例代码3:利用while循环计算1到100之间所有偶数之和

#!/bin/bash
i=2
sum=0
while [ $i -le 100 ]
do
  let sum=sum+$i
  let i+=2
done

echo $sum


2.利用while循环打印**

示例代码:利用while循环打印一个5x5的*

#!/bin/bash
i=1
j=1
while [  $i  -le  5  ]
do
  while [  $j  -le  5  ]
  do
     echo -n  "*  "
     let j++
  done
  echo
  let  i++
  let  j=1

done


3.使用read结合while循环读取文本文件:

示例代码1:

#!/bin/bash
file=$1                  #将位置参数1的文件名复制给file
if [ $# -lt 1 ];then      #判断用户是否输入了位置参数
  echo "Usage:$0 filepath"
  exit
fi
while read -r line   #从file文件中读取文件内容赋值给line(使用参数r会屏蔽文本中的特殊符号,只做输出不做转译)
do

  echo $line        #输出文件内容

done   <  $file



示例2:按列读取文件内容

#!/bin/bash
file=$1
if [[ $# -lt 1 ]]
then
  echo "Usage: $0 please enter you filepath"
  exit
fi
while read -r  f1 f2 f3    #将文件内容分为三列
do
  echo "file 1:$f1 ===> file 2:$f2 ===> file 3:$f3"   #按列输出文件内容

done < "$file"



4.while循环中的死循环:

示例:利用死循环,让用户做选择,根据客户的选择打印相应结果

#!/bin/bash
#打印菜单
while :
do
  echo "********************"
  echo "        menu        "
  echo "1.tima and date"
  echo "2.system info"
  echo "3.uesrs are doing"
  echo "4.exit"
  echo "********************"
  read -p "enter you choice [1-4]:" choice
#根据客户的选择做相应的操作
  case $choice in
   1)
    echo "today is `date +%Y-%m-%d`"
    echo "time is `date +%H:%M:%S`"
    read -p "press [enter] key to continue..." Key    #暂停循环,提示客户按enter键继续
    ;;
   2)
    uname -r
    read -p "press [enter] key to continue..." Key
    ;;
   3)
    w
    read -p "press [enter] key to continue..." Key
    ;;
   4)
    echo "Bye!"
    exit 0
    ;;
   *)
    echo "error"
    read -p "press [enter] key to continue..." Key
    ;;
  esac

done


  • 23
    点赞
  • 124
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Shellwhile循环和for循环是常用的循环结构。 while循环是一个条件控制循环结构,当满足给定条件时,循环体的代码会被执行,直到条件不再满足。while循环的使用场景包括:根据条件循环执行某段代码、遍历文件的内容等。在Shell,可以使用`while`关键字来定义while循环,并在循环体编写相应的代码。需要注意的是,循环体的代码需要有能够改变条件的语句,以免陷入死循环。 for循环是一种遍历循环结构,可以根据指定的条件或范围重复执行一段代码。for循环常用于遍历数组、文件列表等场景。在Shell,可以使用`for`关键字来定义for循环,并在循环体编写相应的代码。具体的循环条件可以使用数字段形式、列出文件列表等方式进行设置。 综上所述,Shellwhile循环和for循环都是常用的循环结构,分别用于满足条件时重复执行一段代码和遍历指定范围的代码。您可以根据具体需求选择适合的循环结构来编写Shell脚本。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Shell的for和while循环详细总结](https://download.csdn.net/download/weixin_38557980/12846206)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Shellwhile循环几种使用实例详解](https://download.csdn.net/download/weixin_38543749/12842313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [微服务项目容器编排docker-compose.yml、Dockerfile文件模板、相关配置文件、shell脚本](https://download.csdn.net/download/qq_45629145/88248761)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值