exit、break、continue
exit退出当前的循环,脚本整体退出
break只管当前的退出
continue结束当前的,但不退出循环
为了更清楚的了解这几个的区别进行以下实验:
[root@node1 mnt]# vim file.sh
文件内容如下:
执行脚本:
[root@node1 mnt]# sh file.sh //正常执行
1
2
3
4
5
hello world!
[root@node1 mnt]# sh file.sh exit //exit退出当前的循环,脚本整体退出
1
2
[root@node1 mnt]# sh file.sh break //break只管当前的退出
1
2
hello world!
[root@node1 mnt]# sh file.sh continue //continue结束当前的,但不退出循环
1
2
4
5
hello world!
题目:执行脚本输出1-10没有4 |
[root@node1 mnt]# vim number_input.sh
内容如下:
执行如下:
{1…5}和`seq 1 5`
{1…5}是1到5,`seq 1 5 `也是1到5,seq可以设定步长
为了更清楚的了解进行以下实验:
[root@node1 mnt]# vim num.sh
文件内容:
执行如下:
[root@node1 mnt]# sh num.sh
1
2
3
4
5
[root@node1 mnt]# vim num.sh //重新编写
文件内容如下:
执行如下:
[root@node1 mnt]# sh num.sh
1
2
3
4
5
[root@node1 mnt]# vim num.sh //重新编写文件内容,seq设定步长为2
文件内容如下:
执行如下:
[root@node1 mnt]# sh num.sh
1
3
5
for语句
for做的是批处理
for
do
done
题目:建立一个数据库,写脚本对所有数据库进行备份操作,每个数据库备份一个文件,以.sql结尾备份到/mnt/mysql_dump |
下载数据库mariadb-server
[root@node1 mnt]# systemctl start mariadb
[root@node1 mnt]# mysql -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database linux;
Query OK, 1 row affected (0.10 sec)
MariaDB [(none)]> use linux
Database changed
MariaDB [linux]> create table linux_user(
-> username varchar(50) not null,
-> password varchar(50) not null);
Query OK, 0 rows affected (0.14 sec)
MariaDB [linux]> insert into linux_user values("user1","123");
Query OK, 1 row affected (0.11 sec)
MariaDB [linux]> insert into linux_user values("user2","234");
Query OK, 1 row affected (0.10 sec)
MariaDB [linux]> select * from linux_user;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user2 | 234 |
+----------+----------+
2 rows in set (0.00 sec)
MariaDB [linux]> quit
Bye
2、编写脚本
[root@node1 mnt]# vim mysql_dump.sh
内容如下:
3、执行脚本
while语句
格式(当条件为真时执行do):
while true
do
done
题目:编写脚本,实时监控根分区的使用情况,超过80%就给超级用户发送一封警告邮件 |
1、编写脚本
[root@node1 mnt]# vim test.sh
内容如下:
2、执行
[root@node1 mnt]# df //查看根分区使用情况
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 9488536 985364 29% /
devtmpfs 469344 0 469344 0% /dev
tmpfs 484932 84 484848 1% /dev/shm
tmpfs 484932 12784 472148 3% /run
tmpfs 484932 0 484932 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2368 451811 1% /home
[root@node1 mnt]# sh test.sh & //执行脚本,并打入后台
[3] 4151
[root@node1 mnt]# mail //查看没有邮件
No mail for root
[root@node1 mnt]# dd if=/dev/zero of=/bigfile bs=1M count=6000
//截取6000M的内存使根分区使用率变成80%以上
[root@node1 mnt]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 9488536 985364 91% /
devtmpfs 469344 0 469344 0% /dev
tmpfs 484932 84 484848 1% /dev/shm
tmpfs 484932 12784 472148 3% /run
tmpfs 484932 0 484932 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2368 451811 1% /home
[root@node1 mnt]# sh test.sh & //执行脚本并打入后台
[3] 4151
[root@node1 mnt]# mail //可以查到邮件
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 23 messages 23 new
>N 1 root Thu Jun 21 02:11 18/590 "warning"
N 2 root Thu Jun 21 02:11 18/590 "warning"
N 3 root Thu Jun 21 02:11 18/590 "warning"
N 4 root Thu Jun 21 02:11 18/590 "warning"
N 5 root Thu Jun 21 02:11 18/590 "warning"
N 6 root Thu Jun 21 02:11 18/590 "warning"
N 7 root Thu Jun 21 02:11 18/590 "warning"
N 8 root Thu Jun 21 02:11 18/590 "warning"
N 9 root Thu Jun 21 02:11 18/590 "warning"
N 10 root Thu Jun 21 02:11 18/590 "warning"
N 11 root Thu Jun 21 02:11 18/590 "warning"
if语句
if then 可以做无限次条件判断
格式:
if
then
elif
then
…
else
fi
题目:用if语句编写一个脚本,用来判断文件类型 |
[root@localhost mnt]# vim check_file.sh
文件内容如下:
#!/bin/bash
if
[ -z "$1" ]
then
echo -e "\033[31merror:please input filename!\033[0m"
elif
[ -L "$1" ]
then
echo the file is link
elif
[ -f "$1" ]
then
echo the file is oridary file
elif
[ -S "$1" ]
then
echo the file is sock
elif
[ -b "$1" ]
then
echo the file is block
elif
[ -d "$1" ]
then
echo the file is dirctory
elif
[ -c "$1" ]
then
echo the file is char
else
echo unknow the file
fi
执行脚本进行测试:
脚本实验:建立用户 1、文件数量不对报错 2、文件不存在报错 3、文件行数差异报错 4、用户存在显示用户存在,但不修改密码 5、当用户不存在,建立用户并设定相应密码 |
[root@desktop mnt]# vim user_create.sh #下面为脚本内容
#!/bin/bash
if
[ "$#" -ne "2" ] #判断脚本后面是否跟两个内容
then
echo please input two file!
exit 0
elif
[ ! -e "$1" ] #判断第一个文件是否存在
then
echo the "$1" in not exist!
exit 0
elif
[ ! -e "$2" ] #判断第二个文件是否存在
then
echo the "$2" in not exist!
exit 0
elif #当脚本后面跟两个内容,且文件存在
num1=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
num2=`awk 'BEGIN{N=0}{N++}END{print N}' $2`
[ "$num1" -ne "$num2" ]
then
echo the file hangshu is no equal
exit 0
else
for i in `seq 1 $num1`
do
username=`sed -n "${i}p" $1`
password=`sed -n "${i}p" $2`
[ -e "/home/$username" ]&&{
echo the $username is exist
}||{
useradd $username
echo $password | passwd --stdin $username
}
done
fi
存放用户名的文件内容:
存放密码的文件内容:
执行脚本如下:
case语句
格式:
case $1 in
字符)
echo
;;
esac
在字符匹配时比if快速。
实验如下:
如下分别为if语句和case语句的内容(根据文件后面所跟字符,输出内容):
执行结果:
脚本实验:在脚本后输入create userfile passfile时建立用户,输入delete userfile时删除用户 |
[root@desktop mnt]# vim user.sh
文件内容如下:
执行如下:
expect
自动应达工具
spawn 监控
expect eof 回答完问题后退出
interact 保留环境不退出,例如:ssh
exp_continue 继续
set NAME [ lindex $argv 0 ] 代表expect后跟的第一串字符赋值给NAME
set OBJ [ lindex $argv 1 ] 代表expect后跟的第二串字符赋值给OBJ
实验:
[root@desktop mnt]# vim question.sh
内容如下:
第一种自动应答脚本:
[root@desktop mnt]# vim answer.sh
内容如下:
执行如下:
第二种自动应答脚本:
[root@desktop mnt]# vim answer1.sh
内容如下:
执行如下:
题目:编写脚本后面跟ip和密码会直接连接其他主机 |
[root@localhost mnt]# vim auto_connect.sh
文件内容如下:
执行如下:
算法
实验如下:
批处理脚本
题目:脚本实验将1-10主机号的主机名导入到文件中 |
[root@localhost mnt]# vim hostname.sh
文件内容如下:
注释:
导入文件后,里面的内容有一行M将其全文替换时用ctrl+和ctrl+M打出^M才可找到
linux系统后面会加\n
windouws会加\n\r
unix后面会加\r
这里出现\r是因为使用的工具grep是unix的(系统兼容性有问题),故有\r
题目:给多个主机批量建立用户 |
[root@localhost mnt]# vim user_create.sh
文件内容如下:
第一种:
第二种:
执行如下脚本后查询建立用户主机的用户是否建立:
[root@localhost mnt]# sh user_create.sh userfile passfile 68 70
#执行脚本方式如上,脚本后跟的第一个是含有用户名的文件,第二个是含有用户密码的文件
68 70 代表的是给172.25.254.68到172.25.254.70的主机建立用户
执行脚本时显示68和70主机 is down只有69主机建立,下面是查询只有69建立的用户是否存在:
题目:用脚本写一个10秒倒计时 |
[root@localhost mnt]# vim time.sh
脚本内容如下:
执行如下:
这里的时间是变化的
题目:用脚本写一个分钟:秒的倒计时 |
[root@localhost mnt]# vim time1.sh
内容如下:
#!/bin/bash
read -p "please input the min: " min #提示输入分钟
read -p "please input the s: " ss #提示输入秒
for ((NUM=$min;NUM>=0;NUM--))
do
[ "$NUM" -ne $min ]&&{
for ((NUM1=59;NUM1>=0;NUM1--)) #正常倒计时应该是60秒,所以应该有一个关于设置起始秒的循环
do
echo -n "after $NUM:$NUM1 is end"
echo -ne "\r \r"
sleep 1
done
}||{
for ((NUM1=$ss;NUM1>=0;NUM1--)) #从输入的每秒开始循环
do
echo -n "after $NUM:$NUM1 is end"
echo -ne "\r \r"
sleep 1
done
}
done
执行如下:
题目: 备份数据库 执行db_dump.sh XXX数据库密码) 脚本执行后会备份数据库中的所有库到/mnt/mysqldump目录中 备份文件名称为“库名称.sql”为此文件存在时报错并询问动作:输入“S”跳过备份,当输入"B"时备份"库名称.sql"文件为"库名称_backup.sql",当输入"O"时覆盖源文件 |
[root@localhost mnt]# vim mysqldump.sh
内容如下:
1、基本内容
2、优化后内容
执行如下: