八、控制循环

break命令

可以退出任意循环

跳出单个循环

[root@tzPC 13Unit]# bash break1.sh 
Iteration number:1
Iteration number:2
Iteration number:3
Iteration number:4
The for loop is completed
[root@tzPC 13Unit]# cat break1.sh 
#!/bin/bash
#breaking out of a for loop
for var1 in 1 2 3 4 5 6 7 8 9 
do 
        if [ $var1 -eq 5 ]
        then
                break
        fi
        echo "Iteration number:$var1"
done
echo "The for loop is completed"

跳出内部循环

break命令默认跳出内部循环,即break 1

[root@tzPC 13Unit]# bash break2.sh 
Outer loop:1
        Inner loop:1
        Inner loop:2
        Inner loop:3
        Inner loop:4
Outer loop:2
        Inner loop:1
        Inner loop:2
        Inner loop:3
        Inner loop:4
Outer loop:3
        Inner loop:1
        Inner loop:2
        Inner loop:3
        Inner loop:4
[root@tzPC 13Unit]# cat break2.sh 
#!/bin/bash
#breaking out of an inner loop
for (( a = 1; a < 4; a++ ))
do
        echo "Outer loop:$a"
        for (( b = 1; b < 100; b++ ))
        do
                if [ $b -eq 5 ]
                then 
                        break
                fi
                echo "  Inner loop:$b"
        done
done

跳出外部循环

break n

n制定了要跳出的循环层级,默认为1,跳出当前循环,如果n设为2,即跳出当前外部一层的循环。

[root@tzPC 13Unit]# bash break3.sh 
Outer loop:1
        Inner loop:1
        Inner loop:2
        Inner loop:3
        Inner loop:4
[root@tzPC 13Unit]# cat break3.sh 
#!/bin/bash
#breaking out of an inner loop
for (( a = 1; a < 4; a++ ))
do
        echo "Outer loop:$a"
        for (( b = 1; b < 100; b++ ))
        do
                if [ $b -gt 4 ]
                then 
                        break 2
                fi
                echo "  Inner loop:$b"
        done
done

 

continue命令

跳出本次循环,进行下次循环,默认相当于continue 1,原理同上

跳出外部循环

[root@tzPC 13Unit]# bash $_
Iteration 1:
        The result of 1 * 1 is 1
        The result of 1 * 2 is 2
Iteration 2:
        The result of 2 * 1 is 2
        The result of 2 * 2 is 4
Iteration 3:
Iteration 4:
        The result of 4 * 1 is 4
        The result of 4 * 2 is 8
Iteration 5:
        The result of 5 * 1 is 5
        The result of 5 * 2 is 10
[root@tzPC 13Unit]# cat $_
#!/bin/bash
#continuing an outer loop
for (( a = 1; a <= 5; a++ ))
do
        echo "Iteration $a:"
        for (( b = 1; b < 3; b++ ))
        do
                if [ $a -gt 2 ] && [ $a -lt 4 ]
                then
                        continue 2
        fi
        var3=$[ $a * $b ]
        echo "  The result of $a * $b is $var3"
        done
done

 处理循环的输出

可以对循环的输出使用管道重定向

[root@tzPC 13Unit]# cat test6.sh
#!/bin/bash
for file in /home/tz1/*
do
        if [ -d "file" ];then
                echo "$file is a directory"
        elif [ -f "$file" ];then
                echo "$file is a file"
        fi
done > output.txt
#或者对结果进行排序
#done | sort

查找可执行文件

思路:从命令行运行一个程序时,系统会搜索环境变量设置的目录,可以遍历PATH变量查找可执行的文件

先使用分隔符分割各个目录

IFS=:
for folder in $PATH
do

在迭代特定目录的所有文件

for file in $folder/*
do

检查文件是否具有可执行权限

if [ -x $file ]
then
     echo "    $file"
fi

具体脚本自己写,哈哈,思路已经有了

创建多个用户账户

思路

创建一个csv表格,收集用户信息

如用户名,用户备注,用逗号分隔,

username,description

 

用read命令读取文件中的各行,read命令会自动读取每一行

while IFS=',' read -r username description

 

创建用户命令,中间循环体

do
echo "adding $username"
useradd -c "$description" -m $username
done <"$input"

 

这里的$input变量的内容是users.csv表格文件,需要提前在循环外定义好,具体自己写

 学习来自:《Linux命令行与Shell脚本大全 第3版》第13章

 

posted @ 2020-07-21 00:19  努力吧阿团  阅读(154)  评论(0编辑  收藏  举报