shell脚本使用教程3
shell脚本使用教程3
一.shell脚本实战
1.监控CPU
#!/bin/bash
US=$(vmstat |awk 'NR==3{print $13}')
SY=$(vmstat |awk 'NR==3{print $14}')
IDLE=$(vmstat |awk 'NR==3{print $15}')
WAIT=$(vmstat |awk 'NR==3{print $16}')
USE=$(($US+$SY))
echo "cpu用户占用: $US%"
echo "cpu系统占用: $SY%"
echo "cpu空闲:$IDLE%"
echo "cpu等待磁盘IO响应占用:$WAIT%"
2.内存
#!/bin/bash
TOTAL=$(free -m |awk '/Mem/{print $2}')
USE=$(free -m |awk '/Mem/{print $3-$6-$7}')
FREE=$(($TOTAL-$USE))
3.判断设备节点是否存在
一般的普通文件的判断条件是 -f。但对于设备节点等特殊文件那就不行了。必须使用与文件类型或者说设备类型相对应的判断条件才可以。
* -b file = True if the file exists and is block special file.块设备文件存在* -c file = True if the file exists and is character special file.字符设备文件存在* -d file = True if the file exists and is a directory.目录文件存在* -e file = True if the file exists.文件存在* -f file = True if the file exists and is a regular file普通文件存在* -g file = True if the file exists and the set-group-id bit is set.文件存在且设置了组标记位* -k file = True if the files' "sticky" bit is set.* -L file = True if the file exists and is a symbolic link.文件存在且是个符号文件* -p file = True if the file exists and is a named pipe.文件存在且是一个命名管道* -r file = True if the file exists and is readable.文件存在且是可读的* -s file = True if the file exists and its size is greater than zero.文件存在
参照上面的说明,我们要判断一个文件存在而不管其类型,那么可以使用 -e,如果要同时检查其类型,那么我们可以使用相对应的判断条件。就拿这个朋友的代码来说,应该使用-e或者-b才是正确的。
#!/bin/sh
check_sda_exist() {echo "Find usb disk......."if [ -b /dev/sda1 ]; thenecho "Runing mount > /udisk"elseecho "Runing WLAN......"fiecho "Mount usb disk to /udisk Success"}
check_sda_exist
4.使用方法usage
#! /bin/bashecho "main.sh runing...."
usage(){cat << EOFUsage:./test.sh -m remake -o allExample:Optional arguments:-h-s-d-bEOF
}
usage
5.grep进程时去除掉本身grep
ps -ef | grep '进程名' | grep -v grep
grep -v grep即去除结果中含有grep的进程。
但是要注意如果你的进程名中本身含有grep也会被忽略。
这时就需要更细致的规则,例如去除条件里加上一个空格:
ps -ef | grep '进程名' | grep -v 'grep '
因为grep进程是以grep+空格开头的,而一般进程不会这样,所以可以区分。
6.确认进程是否起来
#!/bin/shmedia_count=`ps -ef | grep media.codec | grep -v grep | wc -l`
if [ $media_count -eq 1 ]; thenecho "the media.codec process exist"
elseecho "the media.codec process isn't exist"
fi
shell脚本使用教程3相关推荐
- Linux的shell脚本函数教程
Linux的shell脚本函数教程 一.shell介绍 二.shell函数的介绍 三.函数的定义 四.shell中函数使用示例 一.shell介绍 二.shell函数的介绍 三.函数的定义 四.she ...
- Linux Shell脚本入门教程系列之(十六) Shell输入输出重定向
本文是Linux Shell系列教程的第(十六)篇,更多Linux Shell教程请看:Linux Shell系列教程 Shell中的输出和输入的重定向是在使用中经常用到的一个功能,非常实用,今天就为 ...
- Linux Shell脚本入门教程系列之(十五) Shell函数简介
本文是Linux Shell脚本系列教程的第(十五)篇,更多Linux Shell教程请看:Linux Shell脚本系列教程 上一篇之后,函数可以将一个复杂功能划分成若干模块,从而使程序结构更加清晰 ...
- Linux Shell脚本入门教程系列之(十四) Shell Select教程
本文是Linux Shell脚本系列教程的第(十四)篇,更多Linux Shell教程请看:Linux Shell脚本系列教程 在上一篇文章:Linux Shell系列教程之(十三)Shell分支语句 ...
- Linux Shell脚本入门教程系列之(十三)Shell分支语句case … esac教程
本文是Linux Shell脚本系列教程的第(十三)篇,更多Linux Shell教程请看:Linux Shell脚本系列教程 上一篇之 后,分支语句非常实用,基本上高级语言都支持分支语句(pytho ...
- Linux Shell脚本入门教程系列之(十二)Shell until循环
本文是Linux Shell脚本系列教程的第(十二)篇,更多Linux Shell教程请看:Linux Shell脚本系列教程 在上两篇文章Linux Shell系列教程之(十)Shell for循环 ...
- Linux Shell脚本入门教程系列之(十一)Shell while循环
本文是Linux Shell脚本系列教程的第(十一)篇,更多Linux Shell教程请看:Linux Shell脚本系列教程 在上一篇Linux Shell系列教程之(十)Shell for循环中, ...
- Linux Shell脚本入门教程系列之(十)Shell for循环
本文是Linux Shell脚本系列教程的第(十)篇,更多Linux Shell教程请看:Linux Shell脚本系列教程 基本任何语言都有自己的循环语句,Shell当然也不例外,继上一篇之后,今天 ...
- Linux Shell脚本入门教程系列之(九)Shell判断 if else 用法
本文是Linux Shell脚本系列教程的第(九)篇,更多shell教程请看:Linux Shell脚本系列教程 判断语句是每个语言都必不可少的关键语法,Shell命令当然也不例外.继上一篇之后,今天 ...
- Linux Shell脚本入门教程系列之(八)Shell printf命令详解
本文是Linux Shell脚本系列教程的第(八)篇,更多shell教程请看:Linux Shell脚本系列教程 在上一篇:Linux Shell系列教程之(七)Shell输出这篇文章中,已经对She ...
最新文章
- 10只机器狗拉卡车!井然有序,毫不费力 | 极客头条
- redis-5.0.4集群部署
- 说说我为什么看好Spring Cloud Alibaba
- angular1x初始与架构演进(二)
- oracle最新scn补丁,更新:Oracle全面修正了关于DB Link和SCN补丁的公告
- open-capacity-platform环境安装
- composer升级_Composer-命令简介
- 《哪吒》票房超25亿元 进入中国电影票房总榜前十
- android 插件化 androdpluginmgr 扩展开发问题
- 《Effective C#》读书笔记——条目23:理解接口方法和虚方法的区别使用C#表达设计...
- 免疫算法(Immune Algorithm)详解
- python 超像素分割
- java ehcache使用_ehcache使用报错
- 年度Sweb绩效考评表
- “双减”背景下初中数学差异化作业设计研究——海门区“十四五”规划课题开题报告
- Android版本名称、版本号、API level对应关系
- 小学计算机管理员教学计划,小学教学计划汇总六篇
- 华大460 GPIO 例程赏析_20220911
- 天猫送给这些商家“首页置顶”丨 24张PPT详解天猫消费电子2018年商家策略
- 线性判别分析LDA(linear discriminant analysis)与二次判别分析QDA(quadratic discriminant analysis)
热门文章
- 教你如何做好微信营销说到微信营销
- 《人物五官morpher制作工艺》
- 【概率题汇总】互联网公司概率面试题整理
- 计算机整机制造业下滑,多因素导致全球PC出货量下滑 中国增速减缓
- 【JavaScript】阶段性复习
- 怎么退出用户登录linux,怎样登录和退出Linux系统
- win10键盘错乱如何恢复--win7w.com
- linux 命令运行kodi,在Ubuntu/Debian/Raspbian中安装Kodi for Linux的方法
- linux shift f11,然后按下CTRL+SHIFT+F11组合键
- [C语言]输出100以内的所有素数(质数)