1、bash腳本程序執行順序:
順序執行
選擇執行:if、case
循環執行:for、while、until
2、for循環:
格式:
for VAR in list;do
循環體
done
註釋:VAR是變量,此處不要進行引用;list是循環列表;
list的生成方式:
直接給出 //如:1 2 3 4
{start..end} //注意是兩個點,內建列表方式
seq [start [increment]] last //注意seq是一個命令,如果要用需要引用命令執行結果
seq 10
seq 5 10
seq 2 2 10 //步長爲2的整數列表
返回命令的列表
glob
變量引用:如$@,$*
3、while循環:
格式:
while CONDITION;do
循環體
循環控制變量修正表達式
done
註釋:CONDITION爲條件表達式
4、while條件介紹:
進入條件:CONDITION測試條件爲真時進入循環
退出條件:CONDITION測試條件爲假時退出循環
while循環示例:
求100以內所有正整數之和
方法一:
[root@localhost ~]# cat sum_100
#!/bin/bash
sum=0
for i in {1..100};do
sum=$[$sum+$i]
done
echo $sum
[root@localhost ~]#
方法二:
[root@localhost ~]# cat while_sum_100
#!/bin/bash
declare sum=0
declare i=1
while [ $i -le 100 ];do
sum=$[$sum+$i]
let i++
done
echo $sum
[root@localhost ~]#
注意:for是列表循環,如果求1到10000的所有正整數之和,那麼會在內存空間爲這10000個數分別分配內存空間用來存儲數據,這樣是對內存的一種浪費,基於這種考慮,while可能比for循環性能更好些,因爲while循環只有一個變量用來存儲數據。
5、until循環:
格式:
until CONDITION;do
循環體
循環控制變量修正表達式
done
6、until循環條件:
進入條件:CONDITION測試條件爲假時,進入循環
退出條件:CONDITION測試條件爲真時,退出循環
示例:
until循環示例:
求100以內所有正整數之和
[root@localhost ~]# cat until_sum_100
#!/bin/bash
declare -x sum=0
declare -x i=1
until [ $i -gt 100 ];do
sum=$[$sum+$i]
let i++
done
echo $sum
[root@localhost ~]# bash until_sum_100
5050
注意:#bash -x until_sum_100 //檢查腳本執行過程,-x爲調試模式
練習:
1、求100以內所有偶數之和,求100以內所有奇數之和
for循環: [root@localhost ~]# cat for_sum_100_oushujs.sh #!/bin/bash declare sum_ou=0 for i in `seq 2 2 100`;do sum_ou=$[${sum_ou}+$i] done echo sum_ou=${sum_ou} echo " " declare sum_js=0 for j in `seq 1 2 100`;do sum_js=$[${sum_js}+$j] done echo sum_js=${sum_js} echo " " echo sum=$[${sum_ou}+${sum_js}] [root@localhost ~]# [root@localhost ~]# bash for_sum_100_oushujs.sh sum_ou=2550
sum_js=2500
sum=5050 [root@localhost ~]# | while循環: [root@localhost ~]# cat while__sum_oj.sh #!/bin/bash declare sum_ou=0 declare i=0 while [ $i -le 100 ];do sum_ou=$[${sum_ou}+$i] let i=$i+2 done echo sum_ou=${sum_ou} echo " " declare sum_js=0 declare j=1 while [ $j -le 100 ];do sum_js=$[${sum_js}+$j] let j=$j+2 done echo sum_js=${sum_js} echo " " echo sum=$[${sum_ou}+${sum_js}] [root@localhost ~]# [root@localhost ~]# bash while__sum_oj.sh sum_ou=2550
sum_js=2500
sum=5050 [root@localhost ~]# | until循環: [root@localhost ~]# cat until_sum_oj.sh #!/bin/bash declare sum_ou=0 declare i=0 until [ $i -gt 100 ];do sum_ou=$[${sum_ou}+$i] let i=$i+2 done echo sum_ou=${sum_ou} echo " " declare sum_js=0 declare j=1 until [ $j -gt 100 ];do sum_js=$[${sum_js}+$j] let j=$j+2 done echo sum_js=${sum_js} echo " " echo sum=$[${sum_ou}+${sum_js}] [root@localhost ~]# [root@localhost ~]# bash until_sum_oj.sh sum_ou=2550
sum_js=2500
sum=5050 [root@localhost ~]# |
2、創建10個用戶,user101-user110,密碼同用戶名
for循環: [root@localhost ~]# cat for_user10.sh #!/bin/bash for i in user{101..110};do useradd $i echo "$i" | passwd --stdin $i done [root@localhost ~]# [root@localhost ~]# tail /etc/passwd user101:x:2013:2013::/home/user101:/bin/bash user102:x:2014:2014::/home/user102:/bin/bash user103:x:2015:2015::/home/user103:/bin/bash user104:x:2016:2020::/home/user104:/bin/bash user105:x:2017:2021::/home/user105:/bin/bash user106:x:2018:2022::/home/user106:/bin/bash user107:x:2019:2023::/home/user107:/bin/bash user108:x:2020:2024::/home/user108:/bin/bash user109:x:2021:2025::/home/user109:/bin/bash user110:x:2022:2026::/home/user110:/bin/bash [root@localhost ~]# | while循環: [root@localhost ~]# cat while_user10.sh #!/bin/bash for i in {101..110};do userdel user$i echo "user$i del sucess" done echo " " tail -1 /etc/passwd echo " " declare userid=101 while [ $userid -le 110 ];do useradd user${userid} &>/dev/null echo "user${userid}" | passwd --stdin user${userid} &>/dev/null echo "user${userid} add sucess" let userid=$userid+1 done tail -11 /etc/passwd [root@localhost ~]# [root@localhost ~]# bash while_user10.sh user101 del sucess user102 del sucess user103 del sucess user104 del sucess user105 del sucess user106 del sucess user107 del sucess user108 del sucess user109 del sucess user110 del sucess
sh3:x:2012:2012::/home/sh3:/bin/bash
user101 add sucess user102 add sucess user103 add sucess user104 add sucess user105 add sucess user106 add sucess user107 add sucess user108 add sucess user109 add sucess user110 add sucess sh3:x:2012:2012::/home/sh3:/bin/bash user101:x:2013:2013::/home/user101:/bin/bash user102:x:2014:2014::/home/user102:/bin/bash user103:x:2015:2015::/home/user103:/bin/bash user104:x:2016:2020::/home/user104:/bin/bash user105:x:2017:2021::/home/user105:/bin/bash user106:x:2018:2022::/home/user106:/bin/bash user107:x:2019:2023::/home/user107:/bin/bash user108:x:2020:2024::/home/user108:/bin/bash user109:x:2021:2025::/home/user109:/bin/bash user110:x:2022:2026::/home/user110:/bin/bash [root@localhost ~]# | until循環: [root@localhost ~]# cat until_user10.sh #!/bin/bash for i in {101..110};do userdel user$i echo "user$i del sucess" done echo " " tail -1 /etc/passwd echo " " declare userid=101 until [ $userid -gt 110 ];do useradd user${userid} &>/dev/null echo "user${userid}" | passwd --stdin user${userid} &>/dev/null echo "user${userid} add sucess" let userid=$userid+1 done tail -11 /etc/passwd [root@localhost ~]# [root@localhost ~]# bash until_user10.sh user101 del sucess user102 del sucess user103 del sucess user104 del sucess user105 del sucess user106 del sucess user107 del sucess user108 del sucess user109 del sucess user110 del sucess
sh3:x:2012:2012::/home/sh3:/bin/bash
user101 add sucess user102 add sucess user103 add sucess user104 add sucess user105 add sucess user106 add sucess user107 add sucess user108 add sucess user109 add sucess user110 add sucess sh3:x:2012:2012::/home/sh3:/bin/bash user101:x:2013:2013::/home/user101:/bin/bash user102:x:2014:2014::/home/user102:/bin/bash user103:x:2015:2015::/home/user103:/bin/bash user104:x:2016:2020::/home/user104:/bin/bash user105:x:2017:2021::/home/user105:/bin/bash user106:x:2018:2022::/home/user106:/bin/bash user107:x:2019:2023::/home/user107:/bin/bash user108:x:2020:2024::/home/user108:/bin/bash user109:x:2021:2025::/home/user109:/bin/bash user110:x:2022:2026::/home/user110:/bin/bash [root@localhost ~]# |
3、打印九九乘法表
for循環: [root@bogon ~]# cat 99.sh #!/bin/bashfor i in `seq 9`;do for j in `seq $i`;do echo -ne "${i}*${j}=$[${i}*${j}] \t" //-n不輸出換行,-e使反斜槓的解釋 ,\t 水平製表符 done echo done [root@bogon ~]# bash 99.sh 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 [root@bogon ~]# |
while循環 [root@bogon ~]# cat while_99.sh |
until循環 [root@bogon ~]# cat until_99.sh |
4、打印逆序額九九乘法表
for循環: [root@bogon ~]# cat nx_for_99.sh |
while循環: [root@bogon ~]# |
until循環: |