脚本应用思路

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脚本基本应用-实验手册的更多相关文章

  1. Miniconda 安装 & Pip module 安装 & Shell 脚本调用 Miniconda 虚拟环境手册(实战项目应用)

    (实战项目应用) 1. 下载Miniconda 两个安装方式: 方式1:wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Min ...

  2. Shell脚本编程总结及速查手册

    Shell是一种编程语言, 它像其它编程语言如: C, Java, Python等一样也有变量/函数/运算符/if语句/循环控制/… 但在开始之前, 我想先理清Shell语言与Shell之间的关系. ...

  3. 让你提前知道软件开发(22):shell脚本文件操作

    文章1部分 再了解C语言 shell脚本中的文件操作 [文章摘要] 编写shell脚本时,经常会涉及到对文件的操作,比方从文件里读取一行数据.向文件追加一行数据等. 完毕文件读写操作的方法有非常多,了 ...

  4. Linux编程 22 shell编程(输出和输入重定向,管道,数学运算命令,退出脚本状态码)

    1. 输出重定向 最基本的重定向是将命令的输出发送到一个文件中.在bash shell中用大于号(>) ,格式如下:command > inputfile.例如:将date命令的输出内容, ...

  5. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---22

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  6. 2019.11.13课堂实验之用Linux下的shell脚本完成两文本交替输出

    有两个文本如下,实际中并不知道两文本各有多少行: 文本1.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ccccccccccccccccccccccccccc ...

  7. 实验五:shell脚本编程

    项目 内容 这个作业属于哪个课程 班级课程的主页链接 这个作业的要求在哪里 作业要求链接地址 学号-姓名 17043133-木腾飞 作业学习目标 1.了解shell 脚本的概念及应用2.掌握shell ...

  8. 实验五 shell脚本编程

    项目 内容 这个作业属于哪个课程 课程链接 这个作业的要求在哪里 作业要求 学号-姓名 17041428-朱槐健 作业学习目标 1. 了解shell脚本的概念及使用 2.掌握shell脚本语言的基本语 ...

  9. 实验五shell脚本编程

    项目 内容 这个作业属于哪个课程 <班级课程的主页链接> 这个作业的要求在哪里 作业要求链接地址 学号-姓名 17043220-万文文 作业学习目标 1)了解shell脚本的概念及使用.2 ...

随机推荐

  1. Spring整合jdbc编程

    一.Spring对Jdbc的支持    Spring为了提供对Jdbc的支持,在Jdbc API的基础上封装了一套实现,以此建立一个 JDBC 存取框架. 作为 Spring JDBC 框架的核心, ...

  2. select 自动选择 检查下拉列表

    下面我们来看一下selenium webdriver是如何来处理select下拉框的,以Apple注册页面为例. https://appleid.apple.com/cgi-bin/WebObject ...

  3. JS:parseInt("08")或parseInt("09")转换返回0的原因

    一.parseInt用法 parseInt(s); parseInt(s,radix) 二.第一个方式不再多说,第二个方式,radix是s所基于的进制.范围为2-36(不在此范围函数将返回NaN). ...

  4. PHP实现excel导出

    首先去下载PHPExcel类,地址http://phpexcel.codeplex.com/ 方法如下第一步引入这个扩展类 Vendor('PHPExcel'); 第二部就是方法了,下面简单的实现方法 ...

  5. redis 简单命令操作

    一.概述: 在该系列的前几篇博客中,主要讲述的是与Redis数据类型相关的命令,如String.List.Set.Hashes和Sorted-Set.这些命令都具有一个共同点,即所有的操作都是针对与K ...

  6. Js 将 Date 转化为指定格式的String

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  7. Python3.x:正则 re.findall()的用法

    Python3.x:正则 re.findall()的用法 概念: 语法:findall(pattern, string, flags=0) 说明:返回string中所有与pattern相匹配的全部字串 ...

  8. 20145211 《网络渗透》MS08_067安全漏洞

    20145211 <网络渗透>MS08_067安全漏洞 一.实验原理 ms08_067是服务器服务中一个秘密报告的漏洞,于2008年被发现.攻击者利用靶机默认开放的SMB服务的445端口, ...

  9. ubuntu下 gedit中文乱码

    Gedit 3.x 版本设置 (适用于Ubuntu 11.10及以后) 命令方式 gsettings set org.gnome.gedit.preferences.encodings auto-de ...

  10. 前端初级技能No.1 [切图]

    “切图”是指通过测量设计稿,从设计稿中提取图片等方式为页面开发提供支持的过程. 整个“切图”过程主要分为以下五个主要步骤: 分析设计图: 测量元素: 提取图片: 保存图片: 图片优化与合并: 1.分析 ...