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循环: |
转载于:https://blog.51cto.com/10631377/2054544