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 ...
随机推荐
- mysql lock
http://blog.chinaunix.net/uid-21505614-id-289450.html http://bbs.csdn.net/topics/340127237 http://ww ...
- 使用LocationManager来获取移动设备所在的地理位置信息
在Android应用程序中,可以使用LocationManager来获取移动设备所在的地理位置信息.看如下实例:新建android应用程序TestLocation. 1.activity_main.x ...
- 0728am thinkphp介绍
- web安全学习方向~两图胜千言~~
- $聊一聊"驼峰"和"下划线"——Python re.sub函数详细用法实例讲解
日常写代码时候会遇到一些字符串替换的操作,比如把一大堆"驼峰"形式的字符串批量转换成下划线形式."驼峰"形式的变量命名风格在Java中很常见,而下划线形式的变量 ...
- Oracle中用sql语句取随机数和整数
--- 应用round(5.678,3)保留小数 应用floor(5.678)保留整数 应用dbms_random.value(30,50)取得随机数 --- dbms_random包 获得随机小数S ...
- JqGrid 隐藏水平滚动条完美解决方案
我有强迫症,网上找的几个看着就不舒服 不用更改样式表,隐藏最右侧的边框. .ui-jqgrid .ui-jqgrid-bdiv{ overflow-x: hidden; } 不用通过js控制加1px ...
- [HNOI2019]校园旅行(构造+生成树+动规)
题目 [HNOI2019]校园旅行 做法 最朴素的做法就是点对扩展\(O(m^2)\) 发现\(n\)比较小,我们是否能从\(n\)下手减少边数呢?是肯定的 单独看一个颜色的联通块,如果是二分图,我们 ...
- WCF服务类的实例模式(本文为转载)
WCF开发时如何选择正确的实例模式(InstanceMode)? 在使用WCF实例模型时,你是否思考过这几个的问题: ”WCF中的实例模式如何正确应用”? ”使用WCF中的实例模式有何原则可以遵循 ...
- RN初始化环境快速配置
1.安装node node是基于js的,node.js轻量级的Web服务器,想要是React Native跑起来需要node环境,可以去官网下载安装node.js 下载地址:https://nodej ...