shell条件测试及几个小案例
所有测试只会返回0和1当中的一个数值,其中0表示为真,1表示为假
一、检测并创建目录
test -d /全路径/目标目录名称 || mkdir /全路径/目标目录名称
二、条件测试
格式1: test 条件表达式
格式2: [ 条件表达式 ]
格式3: [[ 条件表达式 ]]
常见的有{
[ -e 目标文件名称/目标目录/文件夹名称] 是否在本目录下存在目标目录/文件夹/目标文件名称
[ -d 目标目录/文件夹名称 ] 是否在本目录下存在目标目录/文件夹
[ -f 目标文件名称 ] 是否在本目录下存在目标文件
[ -r 目标文件名称 ] 目标文件读权限
[ -x 目标文件名称 ] 目标文件执行权限
[ -w 目标文件名称 ] 目标文件写权限
[ -L 目标文件名称 ] 目标文件是否为链接文件
}
三、数值比较
数值比较 [ 数值1 操作符 数值2 ]
[ num1 -gt num2 ] 大于 great then
[ num1 -ls num2 ] 小于 less then
[ num1 -eq num2 ] 等于 equals
[ num1 -ne num2 ] 不等于 unequals
[ num1 -ge num2 ] 大于等于 great equals
[ num1 -le num2 ] 小于等于 less equals
四、小型案例:
1.查看指定磁盘使用率,并将其记录到指定文件夹中
{
#!/bin/bash
disk_check=$(df -h|grep "/$" |awk '{print $5}'|awk -F '%' '{print $1}')
test -f 全路径/文件名称.txt || touch 全路径/文件名称.txt
if [ $disk_check -gt 20 ];then
echo "disk use great then 20,it is ${disk_check}%" > 文件名称.txt
else
echo "disk use less then 20,it is $disk_check"
fi
echo $disk_check
}以 bash 文件名称 的方式运行
2、打印当前系统版本、内核、虚拟平台、静态主机
{
#!/bin/bash
System=$(hostnamectl |grep "System"|awk -F ':' '{print $2}')
Kernel=$(hostnamectl |grep "System"|awk -F ':' '{print $2}')
Vt=$(hostnamectl |grep "Virtualization"|awk -F ':' '{print $2}')
Static_Hostname=$(hostnamectl |grep "Static hostname"|awk -F ':' '{print $2}')
echo "当前系统版本: $System"
echo "当前内核版本: $Kernel"
echo "当前虚拟平台: $Vt"
echo "当前静态主机: $Static_Hostname"
}
3、打印当前CPU的负载情况 1分钟 5分钟 15分钟
tip若要查询cpu负载情况可以通过 w 或者 uptime 命令
{
#!/bin/bash
load_1=$(w |awk 'NR==1'|awk -F ':' '{print $4}'|awk -F ',' '{print $1}')
load_5=$(w |awk 'NR==1'|awk -F ':' '{print $4}'|awk -F ',' '{print $2}')
load_15=$(w |awk 'NR==1'|awk -F ':' '{print $4}'|awk -F ',' '{print $3}')
echo -e "\n===========uptime==================="
echo "当前系统1分钟负载是: $load_1"
echo "当前系统5分钟负载是: $load_5"
echo "当前系统15分钟负载是: $load_15"
}
4.打印当前磁盘的所有分区使用状态
{
#!/bin/bash
disk_part=$(df -h|grep "/$"|awk '{print $1}')
disk_total=$(df -h|grep "/$"|awk '{print $2}')
disk_used=$(df -h|grep "/$"|awk '{print $3}')
disk_avail=$(df -h|grep "/$"|awk '{print $4}')
disk_used_per=$(df -h|grep "/$"|awk '{print $5}')
echo -e "\n=============disk info========="
echo "磁盘分区:$disk_part"
echo "磁盘总容量:$disk_total"
echo "磁盘已使用:$disk_used"
echo "磁盘可用:$disk_avail"
echo "磁盘使用百分比:$disk_used_per"
}
5.编写系统工具箱集合
拓展思路:tip 获取系统状态信息{网页->脚本->服务器}->网页实时抓取shell脚本变量对应的数值
Cmmand action
h 显示命令帮助
f 显示磁盘分区
d 显示磁盘挂载
m 查看内存使用
u 查看系统负载
q 退出系统
{
#!/bin/bash
caidan(){
cat <<-EOF
===================
l 显示菜单
h 显示命令帮助
f 显示磁盘分区
d 显示磁盘挂载
m 查看内存使用
u 查看系统负载
q 退出系统
===================
EOF
}
caidan
while true
do
read -p "请输入你想查看系统状态对应码:" str
case $str in
l)
clear
caidan
;;
h)
help|less
;;
f)
lsblk
;;
d)
df -h
;;
m)
free -m
;;
u)
uptime
;;
q)
break
;;
*)
echo "error"
exit 1;
esac
done
}
6.按时间生成文件(2018-05-22.log),将每天的磁盘使用状态写入到对应的文件
分析
1.时间 date+%F
2.磁盘状态 df-h
{
#!/bin/bash
Time=$(date +%F)
df -h > $Time.log
}等价{
#!/bin/bash
df -h > $(date +%F).log
}
7.利用shell编写脚本满实现自动化生成关于计算机进程的日志记录,并且实现将将生成的日志存储在指定目录中:
bash脚本编写名称为test00.sh:
#!/bin/sh
#获取系统当前时间以yyyy-MM-dd表示
Time=$(date +%F)
#获取系统当前时间以yyyyMM表示
TimeOne=$(date +%Y%m)
#判断目标目录是否存在,若不存在则新建目录
test -d /全路径/$TimeOne || mkdir /全路径/$TimeOne
#生成的目标日志名称
file=/全路径/$TimeOne/ps-$Time.log
j_file(){
if [ -f $file ];then
#echo "此日志已经存在,故在已经存在日志的基础上增添当日内容"
ps -ef >>$file
return 0
else
#echo "此日志不存在,故在相应文件夹已经产生此日志"
touch $file
#将新建的日志权限改为只有用户组可读可写
chmod 600 $file
ps -ef >$file
return 1;
fi
}
j_file
编写crontab定时任务
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * */1 * * bash /全路径/test00.sh
:wq
以上是本篇小节,不喜勿喷,感谢理解
相关链接:
【Linux学习(3)】----C语言编程篇(1)_lixxkv的博客-CSDN博客
【Linux学习(2)】----shell编程篇(1)_lixxkv的博客-CSDN博客