手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux Shell编程从初学到精通-循环与结构化命令

[日期:2012-11-23] 来源:Linux社区  作者:uselinux [字体:大 中 小]

循环与结构化命令

最近正在看LinuxShell编程从初学到精通这本书,写的很详细,每一章节后面都有对应的习题,自己也亲手写了下,还有一部分没有写出来,不过正在努力。学习东西要学无止境,循序渐进,希望大家帮助优化下,或者给出更好的建议,谢谢支持!

1、使用for 循环计算100以内所有偶数的和,然后用while循环和until循环来实现这个计算,比较哪种结构更简单;
 
for:
 
 
 
#!/bin/bash
 
#
 
#In addition to sum assigned value
 
sum=0
 
#Use the list for circulation calculated from 1 to 100 of all the sum of, and the value stored in sum
 
for i in {0..100..2}
 
do
 
#    echo $i
 
    let "sum+=i"
 
#    echo $sum
 
done
 
echo "sum=$sum"
 
 
 
while:
 
 
 
 
 
#!/bin/bash
 
#
 
#In addition to sum assigned value
 
sum=0
 
i=0
 
#The use of counter control while circulation calculated from 1 to 100 of all the sum of, and the value stored in sum
 
while (( i <= 100 ))
 
do
 
    let "sum+=i"
 
    echo "sum=$sum"
 
    let "i+=2"
 
    echo "i=$i"
 
done
 
echo "sum=$sum"
 
 
 
until:
 

 

#!/bin/bash
 
#
 
#In addition to sum assigned value
 
sum=0
 
i=0
 
#The use of counter control while circulation calculated from 1 to 100 of all the sum of, and the value stored in sum
 
until (( i > 100 ))
 
do
 
    let "sum+=i"
 
    echo "sum=$sum"
 
    let "i+=2"
 
    echo "i=$i"
 
done
 
echo "sum=$sum"

linux
相关资讯       shell  Linux Shell  shell编程  Linux Shell编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款