赞
踩
当 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
当 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 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
[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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。