神剑山庄资源网 Design By www.hcban.com
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
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无Shell中的while循环几种使用实例详解的评论...
更新日志
2024年10月04日
2024年10月04日
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[320K/MP3][203.44MB]
- 群星.1981-名曲65(2014环球复黑王·百代篇)【EMI百代】【WAV+CUE】
- 陈淑桦.1990-娃娃的故事【柯达】【WAV+CUE】
- 戴梅君.2011-问签诗【美华】【WAV+CUE】
- 戴梅君.2011-问签诗【美华】【WAV+CUE】
- 李国祥.1995-九五变奏【嘉音】【WAV+CUE】
- 许景淳.1992-你来自何方【全美唱片】【WAV+CUE】
- 石欣卉.2007-剧欣卉集·完整电视剧主题精丫华纳】【WAV+CUE】
- 群星.2005-LOVE情歌集VOL.5.2CD【正东】【WAV+CUE】
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】