首页 > 系统相关 >基于linux下的shell中的运算及应用实例

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

时间:2023-01-03 12:38:40浏览次数:36  
标签:shell 实例 sql mnt sh linux input root localhost


运算方式及运算符号:

运算符号  意义( * 标示常用)
+,- 加法,减法
*,/,% 乘法,除法,取余
** 幂运算
++ , -- 自增加,自减少
<,<=,>,>= 比较符号
= , += , -= , *= , /= , %= 赋值运算
例如 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中的运算及应用实例_mysql_02

[root@localhost mnt]# sh for.sh

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

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

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

基于linux下的shell中的运算及应用实例_vim_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中的运算及应用实例_vim_09

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

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

基于linux下的shell中的运算及应用实例_mysql_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中的运算及应用实例_sql_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中的运算及应用实例_vim_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中的运算及应用实例_mysql_14

脚本内容:

基于linux下的shell中的运算及应用实例_sql_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中的运算及应用实例_mysql_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中的运算及应用实例_vim_17

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

基于linux下的shell中的运算及应用实例_vim_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中的运算及应用实例_sql_20


标签:shell,实例,sql,mnt,sh,linux,input,root,localhost
From: https://blog.51cto.com/u_13831562/5985251

相关文章

  • 基于linux下的shell正则表达式(grep,sed,awk)
    正则表达式:正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一......
  • 基于linux下的samba文件共享系统
    SMB文件共享:服务端口:通常使用TCP/445进行所有连接。还使用UDP137,UDP138和TCP/139进行向后兼容。主配置文件:/etc/samba/smb.conf配置环境:准备两台虚拟机,进行配置IP,y......
  • 基于linux下的时间同步
    时间同步:在服务器端共享时间:vim/etc/chrony.conf29localstratum10开启时间共享功能并设定共享级别这个参数开启后本机不去同步别人的时间到本机22allow172.25.254.0/2......
  • 基于Linux下的定时任务
    定时任务:[root@foundation21~]#systemctlstatuscrond.service 首先查看定时服务是否开启[root@foundation21~]#crontab-uroot-e   建立定时任务,注意使用date......
  • 基于Linux下的临时文件的管理
    对临时文件的管理:[[email protected]]#cd/usr/lib/tmpfiles.d/ 切换路径[[email protected]]#vimwestos.conf  [[email protected]]......
  • 基于linux下的shell脚本练习
    shell脚本的简介:打开文本编辑器(可以使用vi/vim命令来创建文件),新建一个文件test.sh,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好,如果你用php写shell脚本,扩......
  • 基于Linux下的yum源的搭建与共享
    yum命令:###yum命令仅仅是对软件进行管理,rpm命令才是真正安装软件的,yum的好处是可以解决软件依赖性。yum源的搭建:mkdir/iso    建立个目录mv/home/kiosk/Desktop/*.is......
  • 基于Linux下的虚拟机安装详解
    首先我们打开一个shell切换到超级用户,输入如下命令:[kiosk@foundation21Desktop]$pwd/home/kiosk/Desktop[kiosk@foundation21Desktop]$su-rootPassword:Lastlogin:F......
  • 利用Barrier使局域网下的Linux(Majaro)和Window10共享同一套键盘鼠标
    项目场景:现有两台电脑:一台笔记本(Win10)连接无线网(DHCP)一台工作站(Linux-Manjaro)连接无线网(DHCP)为了简化桌面、便于操作,打算让笔记本和工作站共用一套键盘鼠标。问题分......
  • Linux下的pip和pip3的冲突问题
    大佬们有啥解决办法还请不吝赐教!前言最近在使用pip命令的时候,总是报如下错误:查询了很多解决方式还是未能解决.但是pip3命令的使用却没任何问题.问题初步解决今天翻看到......