运算方式及运算符号:
运算符号 意义( * 标示常用)
+,- 加法,减法
*,/,% 乘法,除法,取余
** 幂运算
++ , -- 自增加,自减少
<,<=,>,>= 比较符号
例如 a+=1 相当于 a=a+1
SHELL 中常用的运算命令:
运算操作与运算命令 含义
(()) 用与整数运算
let 用于整数运算,与 (()) 类似
expr 用于整数计算,功能相对较多
bc linux下的计算器,适合整数以及小数运算
$[] 用户整数计算
运算命令的简单演示:
[root@localhost mnt]
[root@localhost mnt]
2
[root@localhost mnt]
[root@localhost mnt]
2
[root@localhost mnt]
[root@localhost mnt]
[root@localhost mnt]
2
[root@localhost mnt]
[root@localhost mnt]
1+1
[root@localhost mnt]
2
[root@localhost mnt]
> 1+2
> EOF
3
[root@localhost mnt]
2
[root@localhost mnt]
8
[root@localhost mnt]
6
简单的for语句:
[root@localhost mnt]
[root@localhost mnt]
[root@localhost mnt]
0
1
2
3
4
5
6
7
8
9
for语句实现在1..10里面不输出4:
[root@localhost mnt]
[root@localhost mnt]
1
2
3
5
6
7
8
9
10
[root@localhost mnt]
[root@localhost mnt]
1
2
3
5
6
7
8
9
10
请用运算和已经学过的 shell 语句写一个 10 秒倒计时的脚本:
[root@localhost mnt]
[root@localhost mnt]
请用运算和已经学过的 shell 语句写一个 1 分 10 秒倒计时的脚本:
(脚本用read交互式命令显示倒计时)
[root@localhost mnt]
[root@localhost mnt]# sh countdown1.sh 调用
please input minute:2 交互式输入想执行的几分几秒的倒计时
please input second:10
编写脚本:
利用以上命令制作一个计算器要求如下
执行 Calculator.sh 后显示
请输入您要操作的数字:
请输入要操作的运算:
请输入要操作的第二个数字 :
>> 执行后显示操作后的数值 <<
[root@localhost mnt]
[root@localhost mnt]
please input first int number: 1
please input calculator tactique: + 验证加法
please input second int number: 1.2
2.2
[root@localhost mnt]
please input first int number: 2
please input calculator tactique: * 验证乘法
please input second int number: 3
6
[root@localhost mnt]
please input first int number: 6
please input calculator tactique: / 验证除法
please input second int number: 2.2
2
数据库备份:
执行 db_dump.sh westos( 数据库密码 )
脚本执行后会备份数据库中的所有库到 /mnt/mysqldump 目录
中
备份文件名称为 “库名称 .sql” 当此文件存在时报错并询问动
作
输入“ S” 跳过备份,当输入“ B" 时备份“库名称 .sql” 文件
为“库名称 _backup.sql”, 当输入“ O” 时,覆盖源文件
[root@localhost mnt]
Loaded plugins: langpacks
Package 1:mariadb-server-5.5.35-3.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost mnt]
[root@localhost mnt]
脚本内容:
[root@localhost mnt]# sh db_dump.sh 备份数据库
mysql.sql is backup!!
test.sql is backup!!
[root@localhost mnt]# sh db_dump.sh 再次备份出来提示
[S]kip [B]ackup [O]verwrite
Please input action: b
mysql_backup.sql is backup!!
[S]kip [B]ackup [O]verwrite
Please input action: b
test_backup.sql is backup!!
[root@localhost mnt]# sh db_dump.sh 参数o覆盖备份
[S]kip [B]ackup [O]verwrite
Please input action: o
mysql.sql is overwrite!!
[S]kip [B]ackup [O]verwrite
Please input action: o
test.sql is overwrite!!
[root@localhost mnt]# sh db_dump.sh 参数exit直接退出
[S]kip [B]ackup [O]verwrite
Please input action: exit
bye
[root@localhost mnt]# ls
auto_ssh.sh db_dump.sh host.sh ip_host.list mysqldump
[root@localhost mnt]# cd mysqldump/ 查看备份
[root@localhost mysqldump]# ls
mysql_backup.sql mysql.sql test_backup.sql test.sql
服务自动部署示例:
执行脚本 lamp.sh
脚本执行后部署好论坛,并设定 apache 的网络接口为 8080
自动登陆脚本:
执行 auto_ssh.sh 172.25.254.100 westos
172.25.254.100 为 ip
westos 为密码
执行脚本后自动登陆 172.25.254.100 并保持登陆
[root@localhost mnt]
[root@localhost mnt]
spawn ssh root@172.25.254.84
root@172.25.254.84's password:
Last login: Wed Jun 27 19:25:25 2018
批处理脚本:
检测教室中开启的所有主机,并抓取所有主机的值机名称和 ip
的对应列表,把列表保存在 /mnt/ip_host.list 文件中
[root@localhost mnt]
[root@localhost mnt]# sh host.sh 调用脚本
[root@localhost mnt]# ls
auto_ssh.sh db_dump.sh host.sh ip_host.list mysqldump
[root@localhost mnt]# cat ip_host.list 查看文件
172.25.254.81
172.25.254.82
172.25.254.83
foundation84.ilt.example.com 172.25.254.84
foundation84.ilt.example.com 172.25.254.85