0
点赞
收藏
分享

微信扫一扫

基于linux下的shell中的运算及应用实例

googlefrank 2023-01-03 阅读 105


运算方式及运算符号:

运算符号  意义( * 标示常用)
+,- 加法,减法
*,/,% 乘法,除法,取余
** 幂运算
++ , -- 自增加,自减少
<,<=,>,>= 比较符号
= , += , -= , *= , /= , %= 赋值运算
例如 a+=1 相当于 a=a+1

Shell 中常用的运算命令:

运算操作与运算命令   含义
(()) 用与整数运算
let 用于整数运算,与 (()) 类似
expr 用于整数计算,功能相对较多
bc linux下的计算器,适合整数以及小数运算
$[] 用户整数计算

运算命令的简单演示:

[root@localhost mnt]# ((a=1+1))    (())命令
[root@localhost mnt]# echo $a
2
[root@localhost mnt]# let a=1+1 let命令
[root@localhost mnt]# echo $a
2
[root@localhost mnt]# a=1+1
[root@localhost mnt]# let a=1+1 let命令
[root@localhost mnt]# echo $a
2
[root@localhost mnt]# a=1+1
[root@localhost mnt]# echo $a
1+1
[root@localhost mnt]# echo `expr 1 + 1` expr命令
2
[root@localhost mnt]# bc <<EOF bc计算器命令
> 1+2
> EOF
3
[root@localhost mnt]# echo $[1+1] $[]命令
2
[root@localhost mnt]# echo $[2**3] 幂运算
8
[root@localhost mnt]# echo $[2*3] 乘法运算
6

基于linux下的shell中的运算及应用实例_sql

简单的for语句:

[root@localhost mnt]# ls
[root@localhost mnt]# vim for.sh

基于linux下的shell中的运算及应用实例_sql_02

[root@localhost mnt]# sh for.sh

基于linux下的shell中的运算及应用实例_mysql_03

for语句实现在1…10里面不输出4:

[root@localhost mnt]# vim for.sh   脚本一

基于linux下的shell中的运算及应用实例_mysql_04

[root@localhost mnt]# sh for.sh  调用脚本

基于linux下的shell中的运算及应用实例_vim_05

[root@localhost mnt]# vim for1.sh  脚本二

基于linux下的shell中的运算及应用实例_mysql_06

[root@localhost mnt]# sh for1.sh 调用脚本

基于linux下的shell中的运算及应用实例_vim_07

shell语句写一个 10 秒倒计时的脚本:

[root@localhost mnt]# vim countdown.sh  编写脚本

基于linux下的shell中的运算及应用实例_sql_08

[root@localhost mnt]# sh countdown.sh  调用

基于linux下的shell中的运算及应用实例_mysql_09

shell语句结合read交互式写一个 1 分 10 秒倒计时的脚本:

[root@localhost mnt]# vim countdown1.sh   编写脚本

基于linux下的shell中的运算及应用实例_sql_10

[root@localhost mnt]# sh countdown1.sh  调用
please input minute:2 交互式输入想执行的几分几秒的倒计时
please input second:10

基于linux下的shell中的运算及应用实例_vim_11

编写脚本:

利用以上命令制作一个计算器要求如下
执行 Calculator.sh 后显示
请输入您要操作的数字:
请输入要操作的运算:
请输入要操作的第二个数字 :
>> 执行后显示操作后的数值 <<

[root@localhost mnt]# vim Calculator.sh  编写脚本

基于linux下的shell中的运算及应用实例_vim_12

[root@localhost mnt]# sh Calculator.sh 
please input first int number: 1
please input calculator tactique: + 验证加法
please input second int number: 1.2
2.2
[root@localhost mnt]# sh Calculator.sh
please input first int number: 2
please input calculator tactique: * 验证乘法
please input second int number: 3
6
[root@localhost mnt]# sh Calculator.sh
please input first int number: 6
please input calculator tactique: / 验证除法
please input second int number: 2.2
2

基于linux下的shell中的运算及应用实例_mysql_13

数据库备份:

执行 db_dump.sh westos( 数据库密码 )
脚本执行后会备份数据库中的所有库到 /mnt/mysqldump 目录

备份文件名称为 “库名称 .sql” 当此文件存在时报错并询问动

输入“ S” 跳过备份,当输入“ B" 时备份“库名称 .sql” 文件
为“库名称 _backup.sql”, 当输入“ O” 时,覆盖源文件

[root@localhost mnt]# yum install mariadb-server -y   安装数据库
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]# systemctl start mariadb 开启数据库
[root@localhost mnt]# vim db_dump.sh 编辑脚本

基于linux下的shell中的运算及应用实例_vim_14

脚本内容:

基于linux下的shell中的运算及应用实例_mysql_15

[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

基于linux下的shell中的运算及应用实例_sql_16

服务自动部署示例:

执行脚本 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]# vim auto_ssh.sh

基于linux下的shell中的运算及应用实例_mysql_17

[root@localhost mnt]# sh auto_ssh.sh 172.25.254.84 westos
spawn ssh root@172.25.254.84
root@172.25.254.84's password:
Last login: Wed Jun 27 19:25:25 2018

基于linux下的shell中的运算及应用实例_sql_18

批处理脚本:

检测教室中开启的所有主机,并抓取所有主机的值机名称和 ip
的对应列表,把列表保存在 /mnt/ip_host.list 文件中

[root@localhost mnt]# vim host.sh  编写脚本

基于linux下的shell中的运算及应用实例_sql_19

[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 查看文件

基于linux下的shell中的运算及应用实例_mysql_20


举报

相关推荐

0 条评论