1-22-shell脚本基本应用-实验手册
脚本应用思路
1. 确定命令操作(设计并执行任务)
2. 编写Shell脚本(组织任务过程)
3. 设置计划任务(控制时间,调用任务脚本)
---------------------------------------------
实战-1、实现管理员在登录系统时显示如下信息
1)当前运行的进程数量
2)当前系统的登录用户的数量
3)当前使用的磁盘根分区的使用情况
操作过程如下图:

注意:也可修改/etc/profile配置文件,实现开机运行
脚本源码如下:
#!/bin/bash
# welcome information for login
#
# Print below information:
# 1. The total number of processes.
# 2. The totla number of users who logged in.
# 3. The used status of disk.
echo "########## Welcome! ##########"
# 1. The total number of processes.
echo "The total number of processes is $(ps -aux | wc -l)."
# 2. The totla number of users who logged in.
echo "The totla number of users who logged in is $(who | wc -l)."
# 3. The used status of disk.
echo "The used status of disk is $(df -hT | grep root | awk '{print $6}')."
---------------------------------------------
实战-2、编写脚本实现系统服务启动 停止 当前状态的脚本
#!/bin/bash
#service manager script
# status start stop restart
if [ $1 = 'start' ] #start server of $2
then systemctl start $2 2>>server.logs
elif [ $1 = 'stop' ] #stop server of $2
then systemctl stop $2 2>>server.logs
elif [ $1 = 'status' ] #status server of $2
then systemctl status $2 2>>server.logs
elif [ $1 = 'restart' ] #restart server of $2
then systemctl restart $2 2>>server.logs
fi
---------------------------------------------
实战-3、写运行状况监控脚本
/sh/monitor.sh,用于记录CPU负载、内存和交换空间、磁盘空间、最近的用户登录情况等信息,以及当时的时间信息。

#!/bin/bash
#back monitor CPU Mem Swap Disk LastLogin information to file
# set recored Dir
Dir=/var/log/runrec/
# prepare the environment
mkdir -p $Dir
# get current time
RecTime=$(date +"%Y-%m-%d %H:%M")
# set record filename
RecFile=$Dir$RecTime.log
# get CPU
RecLoad=$(uptime)
# get Mem and Swap
RecMem=$(free -m)
# get Disk
RecDisk=$(df -hT)
# get Last
RecLastLogin=$(last -n 20)
# echo information and saved to RecFile
echo "######################################
Cpu Load information:$RecLoad
Memory information:$RecMem
Disk Usage information:$RecDisk
Last Login 20 users record:$RecLastLogin" >> $RecFile
设置定时备份:

---------------------------------------------
实战-4、过滤出本机echo网卡的MAC地址,并赋值给hwaddr

[root@xiaogan121 sh]# ifconfig | grep ether | awk '{ print $2 }'
00:0c:29:af:03:b2
00:0c:29:af:03:bc
52:54:00:83:20:8c
[root@xiaogan121 sh]# ip=$(ifconfig | grep ether | awk '{ print $2 }')
[root@xiaogan121 sh]# echo $ip
00:0c:29:af:03:b2 00:0c:29:af:03:bc 52:54:00:83:20:8c
[root@xiaogan121 sh]#
---------------------------------------------
实战-5、编写脚本计算当前的内存使用百分比
MemTotal=$(free -m | grep Mem | awk '{ print $2 }')
MemUse=$(free -m | grep Mem | awk '{ print $3 }')
usage=$( expr $( expr $MemUse \* 100 ) / $MemTotal )

[root@xiaogan121 sh]# usage=$( expr $( expr $(free -m | grep Mem | awk '{ print $3 }') \* 100 ) / $(free -m | grep Mem | awk '{ print $2 }') )
[root@xiaogan121 sh]# echo $usage
31
[root@xiaogan121 sh]# free -m | grep Mem | awk '{ print $3 }'
311
[root@xiaogan121 sh]# free -m | grep Mem | awk '{ print $2 }'
977
[root@xiaogan121 sh]# free -m
total used free shared buff/cache available
Mem: 977 312 292 7 373 474
Swap: 2047 0 2047
[root@xiaogan121 sh]#
---------------------------------------------
实战-6、计算3 4 的平方和

1-22-shell脚本基本应用-实验手册的更多相关文章
- Miniconda 安装 & Pip module 安装 & Shell 脚本调用 Miniconda 虚拟环境手册(实战项目应用)
(实战项目应用) 1. 下载Miniconda 两个安装方式: 方式1:wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Min ...
- Shell脚本编程总结及速查手册
Shell是一种编程语言, 它像其它编程语言如: C, Java, Python等一样也有变量/函数/运算符/if语句/循环控制/… 但在开始之前, 我想先理清Shell语言与Shell之间的关系. ...
- 让你提前知道软件开发(22):shell脚本文件操作
文章1部分 再了解C语言 shell脚本中的文件操作 [文章摘要] 编写shell脚本时,经常会涉及到对文件的操作,比方从文件里读取一行数据.向文件追加一行数据等. 完毕文件读写操作的方法有非常多,了 ...
- Linux编程 22 shell编程(输出和输入重定向,管道,数学运算命令,退出脚本状态码)
1. 输出重定向 最基本的重定向是将命令的输出发送到一个文件中.在bash shell中用大于号(>) ,格式如下:command > inputfile.例如:将date命令的输出内容, ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---22
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 2019.11.13课堂实验之用Linux下的shell脚本完成两文本交替输出
有两个文本如下,实际中并不知道两文本各有多少行: 文本1.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ccccccccccccccccccccccccccc ...
- 实验五:shell脚本编程
项目 内容 这个作业属于哪个课程 班级课程的主页链接 这个作业的要求在哪里 作业要求链接地址 学号-姓名 17043133-木腾飞 作业学习目标 1.了解shell 脚本的概念及应用2.掌握shell ...
- 实验五 shell脚本编程
项目 内容 这个作业属于哪个课程 课程链接 这个作业的要求在哪里 作业要求 学号-姓名 17041428-朱槐健 作业学习目标 1. 了解shell脚本的概念及使用 2.掌握shell脚本语言的基本语 ...
- 实验五shell脚本编程
项目 内容 这个作业属于哪个课程 <班级课程的主页链接> 这个作业的要求在哪里 作业要求链接地址 学号-姓名 17043220-万文文 作业学习目标 1)了解shell脚本的概念及使用.2 ...
随机推荐
- Java基础知识陷阱系列
Java基础知识陷阱系列 今天抽空把Java基础知识陷阱有关的文章汇总于此,便于大家查看. Java基础知识陷阱(一) Java基础知识陷阱(二) Java基础知识陷阱(三) Java基础知识陷阱(四 ...
- LinQ高级查询、组合查询、IQueryable集合类型
LinQ高级查询: 1.模糊查询(包含) Repeater1.DataSource = con.car.Where(r =>r.name.Contains(s)).ToList(); 2.开头 ...
- selenim之ActionChains 用法
常见的点击方法集锦: 参数: 1.driver是我们的浏览器 2.Actions是我们系统内置的执行鼠标一系列操作的对象 鼠标左击:Actions actions=new Actions(driver ...
- asp.net Mvc 使用uploadify 上传文件 HTTP 302 Error
CSHTML代码 @{ if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) { <input type=" ...
- Django学习笔记之Web框架由浅入深和第一个Django实例
Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 半成品自定义web框架 impor ...
- 延迟环境变量扩展(bat)
延迟环境变量扩展(bat) 之前遇到一些环境变量的问题,简单记录下 From:http://www.cnblogs.com/dongzhiquan/archive/2012/09/05/2671218 ...
- hadoop随手笔记
1.Hadoop Streaming 是为了方便不太熟悉java用户编写MR程序的工具.用户可以将任何可执行文件(C++)或者脚本(python,ruby)作为Mapper/Reducer, 提高了效 ...
- Python Matplotlib简易教程【转】
本文转载自:https://blog.csdn.net/Notzuonotdied/article/details/77876080 详情请见:Matplotlib python 数据可视化神器 简单 ...
- # fabirc 配置多组服务器 密码与密钥一起使用 key_filename的设置
环境说明 myv myv2 是配置在/etc/hosts 的两台 虚拟机 虚拟机ip. 参考英文文档 官方文档的例子不是给的很详细.. http://docs.fabfile.org/en/1.13/ ...
- Commons之Commons-io
转载自(https://my.oschina.net/u/2000201/blog/480071) 1 概述 Commons IO是针对开发IO流功能的工具类库. 主要包括六个区域: 工具类——使用 ...