0%

Shell循环语句

Shell循环语句

for循环基本使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#取值列表有多种取值方式,可以直接读取in后面的值,默认以空格做分割符
语法
for i in 变量的值
do
循环体
done

#出现值中间有空白字符
[root@shell day04]# cat for-1.sh
#!/bin/bash
for i in file1 "file2 dir" file3
do
echo $i
done



#特殊符号

[root@shell day04]# cat for-1.sh
#!/bin/bash
for i in file1 \"file2 dir file3
do
echo $i
done


#从变量中取值


[root@shell day04]# cat for-1.sh
#!/bin/bash
list="file1 file2 dir file3"
for i in $list
do
echo $i
done


#从文件中取值


[root@shell day04]# cat for-2.sh
#!/bin/bash
for i in $(cat /etc/hosts)
do
echo $i
done


#给for定义指定分隔符

[root@shell day04]# cat for-2.sh
#!/bin/bash
IFS=:
for i in $(cat /etc/passwd | head -1)
do
echo $i
done

#以换行符为分隔符
[root@shell day04]# cat for-2.sh
#!/bin/bash
IFS=$'\n'
for i in $(cat /etc/hosts)
do
echo $i
done

for循环基本使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#以冒号做分隔符                         IFS=:


#以冒号,分号和双引号作为字段分隔符 IFS=:;"


#以换行符作为字段分隔符 IFS=$'\n'


#以回车为换行符
[root@qiudao /scripts]# cat for-6.sh
#!/bin/bash
IFS=$'\n'
for var in `cat /etc/hosts`
do
echo $var
done
[root@qiudao /scripts]# sh for-6.sh
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6


#以:为分隔符
[root@qiudao /scripts]# cat for-7.sh
#!/bin/bash
IFS=:
list=`head -1 /etc/passwd`
for var in $list
do
echo $var
done
[root@qiudao /scripts]# sh for-7.sh
root
x
0
0
root
/root
/bin/bash

使用for循环实现数据库的分库分表备份。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
1.怎么备份数据库    
mysqldump -uroot -p123.com -B world > world_database.sql
2.怎么备份数据库的表
mysqldump -uroot -p123.com world city > world_city_tables.sql
3.备份到哪儿
/mysql_dump/oldboy/city_2019_07_16.sql



[root@shell day05]# cat for-3.sh
#!/bin/bash
Db_user=root
Db_pass=123456
Db_name=$(mysql -u$Db_user -p$Db_pass -e "show databases;" |sed 1d |grep -v ".*_schema")
Date=$(date +%F)

for database in $Db_name
do
if [ ! -d /backup/$database ];then
mkdir -p /backup/$database
fi
mysqldump -u$Db_user -p${Db_pass} --single-transaction -B $database >/backup/$database/${database}_${Date}.sql
if [ $? -eq 0 ];then
echo "数据库 $database 备份成功!"
Db_table=$(mysql -u$Db_user -p${Db_pass} -e "use $database;show tables;" |sed 1d)
for tables in $Db_table
do
mysqldump -u$Db_user -p${Db_pass} --single-transaction $database $tables >/backup/$database/${database}_${tables}_${Date}.sql
if [ $? -eq 0 ];then
echo "数据库$database 中的表 $tables 备份成功!"
else
echo "数据库$database 中的表 $tables 备份失败!"
continue
fi
done
else
echo "数据库 $database 备份失败!"
continue
fi
done

循环语句while基本概述

1
2
3
4
5
6
7
8
while循环语句,只要条件成立就反复执行对应的命令操作,直到命令不成立或为假
#当条件测试成立(条件测试为真),执行循环体

while 条件测试
do
循环体

done

while循环基本使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#降序输出10到1的数字
[root@qiudao /scripts]# cat while-1.sh
#!/bin/bash
var=10
while [ $var -gt 0 ]
do
echo $var
var=$[$var-1]
done


#简单加法表
[root@qiudao /scripts]# cat while-2.sh
#!/usr/bin/bash


a=1
b=10
while [ $a -le 10 ]
do
sum=$(( $a + $b ))
echo $a + $b = $sum
let a++
let b--
done


#两数相乘
#自增
[root@qiudao /scripts]# cat while-3.sh
#!/bin/bash
num=9
while [ $num -ge 1 ]
do
sum=$(( $num * $num ))
echo "$num * $num = $sum"
num=$[$num-1]
done


#自减
[root@qiudao /scripts]# cat while-4.sh
#!/bin/bash
num=1
while [ $num -le 9 ]
do
sum=$(( $num * $num ))
echo "$num * $num = $sum"
num=$[$num+1]
done

猜数字游戏脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
1) 随机输出一个1-100的数字 echo $((RANDOM % 100 + 1 )) 
2) 要求用户输入的必须是数字(数字处加判断)

3) 如果比随机数小则提示比随机数小了
大则提示比随机数大了

4) 正确则退出
错误则继续死循环

5) 最后统计总共猜了多少次(成功多少次,失败多少次)


#!/usr/bin/bash
sj=$(echo $((RANDOM % 100 + 1 )))
i=0
while true
do
read -p "请输入一个你需要猜的数字: " cc
if [[ ! $cc =~ ^[0-9]+$ ]];then
echo "请输入整数"
continue
fi


#将用户的输入的数字与随机数进行比较

if [ $cc -gt $sj ];then
echo "你猜的太大"
elif [ $cc -lt $sj ];then
echo "你猜的太小,继续加油"
else
echo "猜对了....."
break
fi
#进行数值自增

let i++
done
echo "你总共失败了多少次 $i"
echo "你总共猜了多少次 $(( $i + 1 ))"

跳出循环语句指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
01. exit,退出整个程序

[root@qiudao /scripts]# cat for-exit.sh
#!/bin/bash
for i in {1..3}
do
echo "123"
exit
echo "456"
done
echo "done ......"


#执行结果
[root@qiudao /scripts]# sh for-exit.sh
123
-----------------------------------------------------------------------------------------

02. break,结束当前循环,或跳出本层循环

[root@qiudao /scripts]# cat for-break.sh
#!/bin/bash
for i in {1..3}
do
echo "123"
break
echo "456"
done
echo "done ......"


#执行结果
[root@qiudao /scripts]# sh for-break.sh
123
done ......
-----------------------------------------------------------------------------------------

03. continue,忽略本次循环剩余的代码,直接进行下一次循环。


[root@qiudao /scripts]# cat for-continue.sh
#!/bin/bash
for i in {1..3}
do
echo "123"
continue
echo "456"
done
echo "done ......"


#执行结果
[root@qiudao /scripts]# sh for-continue.sh
123
123
123
done ......