25 shell编程 循环语句 for循环 while循环 until循环 案例;批量解压文件

在这里插入图片描述

while do done

当 condition 条件成立时,就进行循环,直到 condition 的条件不成立才停止

while [ condition ]  <==中括号内的状态就是判断式
do            <==do 是循环的开始!
	程序段落
done          <==done 是循环的结束
[userwin@MiWiFi-R3L-srv sh]$ vim while1.sh 

#!/bin/bash
while [ "$yn" != "yes" -a "$yn" != "YES" ]
do
        read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."

[userwin@MiWiFi-R3L-srv sh]$ sh while1.sh 
Please input yes/YES to stop this program: a
Please input yes/YES to stop this program: b
Please input yes/YES to stop this program: yes
OK! you input the correct answer.
[userwin@MiWiFi-R3L-srv sh]$ vim while2.sh 

#!/bin/bash

# 1加到100 求和
i=1
s=0
while [ $i -le 100 ]
do
        s=$(( $s+$i ))
        i=$(( $i+1 ))
done
echo "the sum is $s"

[userwin@MiWiFi-R3L-srv sh]$ sh while2.sh 
the sum is 5050

until do done

当 condition 条件成立时,就终止循环, 否则就持续进行循环的程序段。

until [ condition ]
do
	程序段落
done
[userwin@MiWiFi-R3L-srv sh]$ vim until1.sh 

#!/bin/bash
until [ "$yn" == "yes" -o "$yn" == "YES" ]
do
        read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."
[userwin@MiWiFi-R3L-srv sh]$ sh until1.sh 
Please input yes/YES to stop this program: a
Please input yes/YES to stop this program: b
Please input yes/YES to stop this program: YES
OK! you input the correct answer.

[userwin@MiWiFi-R3L-srv sh]$ vim until2.sh 

#!/bin/bash

# 1加到100 求和
i=1
s=0
until [ $i -gt 100 ]
do
        s=$(( $s+$i ))
        i=$(( $i+1 ))
done
echo "the sum is $s"

[userwin@MiWiFi-R3L-srv sh]$ sh until2.sh 
the sum is 5050

for…do…done

for var in con1 con2 con3 ...
do
	程序段
done

场景一

[userwin@MiWiFi-R3L-srv sh]$ vim for1.sh 

#!/bin/bash
for animal in dog cat elephant
do
        echo "There are ${animal}s.... "
done

[userwin@MiWiFi-R3L-srv sh]$ sh for1.sh 
There are dogs.... 
There are cats.... 
There are elephants.... 

场景二

[userwin@MiWiFi-R3L-srv sh]$ vim for2.sh 

#!/bin/bash

users=$(cut -d ':' -f1 /etc/passwd)  # 撷取帐号名称
for username in $users               # 开始回圈进行!
do
        echo "username : ${username}"
done

[userwin@MiWiFi-R3L-srv sh]$ sh for2.sh 
username : root
username : bin
username : daemon
username : adm
username : lp
username : sync
username : shutdown

场景三

[userwin@MiWiFi-R3L-srv sh]$ vim for3.sh 

#!/bin/bash
for index in $(seq 1 100)       # seq 为 sequence(连续) 的缩写之意
do
        echo "index : $index"
done
[userwin@MiWiFi-R3L-srv sh]$ sh for3.sh 
index : 1
index : 2
index : 3
index : 4
index : 5
index : 6
index : 7
index : 8
index : 9
index : 10
index : 11
index : 12
index : 13
index : 14
index : 15
index : 16
index : 17
index : 18
index : 19
index : 20
index : 21
index : 22
index : 23
index : 24
index : 25
index : 26
index : 27
index : 28
index : 29
index : 30
index : 31
index : 32
index : 33
index : 34
index : 35
index : 36
index : 37
index : 38
index : 39
index : 40
index : 41
index : 42
index : 43
index : 44
index : 45
index : 46
index : 47
index : 48
index : 49
index : 50
index : 51
index : 52
index : 53
index : 54
index : 55
index : 56
index : 57
index : 58
index : 59
index : 60
index : 61
index : 62
index : 63
index : 64
index : 65
index : 66
index : 67
index : 68
index : 69
index : 70
index : 71
index : 72
index : 73
index : 74
index : 75
index : 76
index : 77
index : 78
index : 79
index : 80
index : 81
index : 82
index : 83
index : 84
index : 85
index : 86
index : 87
index : 88
index : 89
index : 90
index : 91
index : 92
index : 93
index : 94
index : 95
index : 96
index : 97
index : 98
index : 99
index : 100

场景四

# 查看目录是否存在
read -p "Please input a directory: " dir
if [ "$dir" == "" -o ! -d "$dir" ]; then
	echo "The $dir is NOT exist in your system."
	exit 1
fi

# 存在; 则循环目录下的文件
filelist=$(ls $dir)        # 列出所有在该目录下的文件名称
for filename in $filelist
do
	perm=""
	test -r "$dir/$filename" && perm="$perm readable"
	test -w "$dir/$filename" && perm="$perm writable"
	test -x "$dir/$filename" && perm="$perm executable"
	echo "The file $dir/$filename's permission is $perm "
done

for…do…done 的数值处理


for (( 初始值; 限制值; 运行步阶 ))
do
	程序段
done
[userwin@MiWiFi-R3L-srv sh]$ vim for4.sh 

#!/bin/bash
read -p "Please input a number, I will count for 1+2+...+your_input: " nu

s=0
for (( i=1; i<=$nu; i=i+1 ))
do
        s=$(($s+$i))
done
echo "The result of '1+2+3+...+$nu' is ==> $s"
[userwin@MiWiFi-R3L-srv sh]$ sh for4.sh 
Please input a number, I will count for 1+2+...+your_input: 100
The result of '1+2+3+...+100' is ==> 5050

# 批量解压文件
[userwin@MiWiFi-R3L-srv sh]$ vim for4.sh 

#!/bin/bash

cd $HOME/targzs
ls *.tar.gz>ls.log
for tarFlie in $(cat ls.log)
do
	tar -zxf $tarFile &>/dev/null
done
rm -rf $HOME/targzs/ls.log
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EngineerForSoul

你的鼓励是我孜孜不倦的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值