12.3、bash脚本循环语句

前端之家收集整理的这篇文章主要介绍了12.3、bash脚本循环语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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 -xuntil_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、打印九九乘法表








4、打印逆序额九九乘法表

原文链接:https://www.f2er.com/bash/389192.html

猜你在找的Bash相关文章