这2天整理脚本,准备将一个脚本优化成可适用于多个平台的通用脚本。
实验脚本
显示温限数据
echo "temperature up = `cat /sys/devices/virtual/thermal/thermal_zone0/trip_point_1_temp`" | tee -a $logname
while true
do
echo "**********************************************************"
temperature=`cat /sys/class/thermal/thermal_zone0/temp`
temperature=`echo "scale=3;$temperature/1000" | bc`
echo -e "\033[42;37m `date +%T` current temperature= $temperature runtime= `cat /proc/uptime` \033[0m " | tee -a $logname
echo " `date +%T` current freq= `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq`" | tee -a $logname
vmstat 1 3 | tee -a $logname
sleep ${delaytime}s
echo ""
done
遇到问题
当在不同的平台上运行的时候,发现不同的系统在裁剪的时候会不一样,可能bc命令在有的系统中存在,有的系统中不存在,那么我就要在这里预判以下结果。
-
使用命令执行方式 ,发现没有成功,无论如何都不能让错误信息不显示
-
使用 find 方式
bcflag=find / -name bc | grep bc | wc -l
这样可以判断bc命令是否存在,缺点是:有点慢,毕竟查找需要时间,另外,如果存在其他带bc的其他文件也会得到错误的结果。不是很完美 -
使用command方式
bcflag=command -v bc | wc -l
这样,如果存在bc命令,会显示出文件的位置,没有就为空
这样就可以判断,当为0的时候,就没有发现bc的命令,使用别的方式来实现
完整脚本
Logname=“./run.log”
# 显示温限数据
echo "temperature up = `cat /sys/devices/virtual/thermal/thermal_zone0/trip_point_1_temp`" | tee -a $logname
# 判断某个命令是否存在可使用command -v 或者find来实现
# bcflag=`find / -name bc | grep bc | wc -l`
bcflag=`command -v bc | wc -l`
vmstatflag=`command -v vmstat | wc -l`
while true
do
echo "**********************************************************"
temperature=`cat /sys/class/thermal/thermal_zone0/temp`
if [ $bcflag -eq 0 ]
then
temperature=`expr $temperature / 1000 ` # 1103 没有bc程序
else
temperature=`echo "scale=3;$temperature/1000" | bc`
fi
echo -e "\033[42;37m `date +%T` current temperature= $temperature runtime= `cat /proc/uptime` \033[0m " | tee -a $logname
echo " `date +%T` current freq= `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq`" | tee -a $logname
#由于1103 不支持vmstat 使用top代替
if [ $vmstatflag -eq 0 ]
then
echo " `date +%T` `top -b -n 1 | grep 'Mem' -A 1 -B 1`" | tee -a $logname
else
vmstat 1 3 | tee -a $logname
fi
sleep ${delaytime}s
echo ""
done
创作不易,点个赞在走呗、欢迎关注。
–好记性不如烂笔头。