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 ...
随机推荐
- 模块讲解----json模块(跨平台的序列化与反序列化)
一.json的特点 1.只能处理简单的可序列化的对象:(字典,列表,元祖) 2.json支持不同语言之间的数据交互:(python - go,python - java) 二.使用场景 1.玩 ...
- 在MFC下如何定义全局变量和全局函数
用MFC制作的工程由很多文件构成,它不能象一般C++程序那样随意在类外定义全局变量,在这里要想定义能被工程内多个文件共享的全局变量和函数必须用一些特殊方法才行.实际上有多种方法可以实现,这里只介绍两种 ...
- Mock Server 之 moco-runner 使用指南二
文章出处http://blog.csdn.net/crisschan/article/details/53335234 moco-runner 安装配置 1. 下载jar https://repo1. ...
- 19. Remove Nth Node From End of List(移除倒数第N的结点, 快慢指针)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 乐观锁的一种实现方式——CAS
在java里面,synchronized关键字就是一种悲观锁,因为在加上锁之后,只有当前线程可以操作变量,其他线程只有等待. CAS操作是一种乐观锁,它假设数据不会产生冲突,而是在提交的时候再进行版本 ...
- 使用Spring Session和Redis解决分布式Session跨域共享问题
http://blog.csdn.net/xlgen157387/article/details/57406162 使用Spring Session和Redis解决分布式Session跨域共享问题
- mongo启动
mongo启动 删除data目录里的mongo.lock bin 目录里执行 net start MongoDB
- C/C++:函数的编译方式与调用约定以及extern “C”的使用
转自:https://www.cnblogs.com/qinfengxiaoyue/archive/2013/02/04/2891908.html 函数在C++编译方式与C编译方式下的主要不同在于:由 ...
- linux 登录数据库
-h 地址 (例如:127.0.0.1,localhost) mysql -h 127.0.0.1 -u root -p
- 在Pycharm中配置Github
Pycharm是当前进行python开发,尤其是Django开发最好的IDE.GitHub是程序员的圣地,几乎人人都在用. 本文假设你对pycharm和github都有一定的了解,并且希望在pycha ...