歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Linux Shell編程從初學到精通-循環與結構化命令

Linux Shell編程從初學到精通-循環與結構化命令

日期:2017/3/1 10:06:48   编辑:SHELL編程

循環與結構化命令

最近正在看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"

Copyright © Linux教程網 All Rights Reserved