嵌入式系统之判断命令是否存在

这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命令在有的系统中存在,有的系统中不存在,那么我就要在这里预判以下结果。

  1. 使用命令执行方式 ,发现没有成功,无论如何都不能让错误信息不显示

  2. 使用 find 方式
    bcflag=find / -name bc | grep bc | wc -l
    这样可以判断bc命令是否存在,缺点是:有点慢,毕竟查找需要时间,另外,如果存在其他带bc的其他文件也会得到错误的结果。不是很完美

  3. 使用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

创作不易,点个赞在走呗、欢迎关注。

–好记性不如烂笔头。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

six2me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值