• Shell下syntax error: operand expected (error token is “-”)


    在这个监控实时网口速率的脚本中,第21,22行存在错误:

    #!/bin/bash
    #Modified by lifei4@datangmobile.cn
    echo ===DTmobile NetSpeedMonitor===
    sleep 1
    echo loading...
    sleep 1
    
    ethn=$1
     
    while true
    do
      RXpre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
      TXpre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
      sleep 1
      RXnext=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
      TXnext=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
     
      clear
      echo -e "			  RX 		   TX  			 TIME"
     
      RX=$((${RXnext}-${RXpre}))
      TX=$((${TXnext}-${TXpre}))
     
      if [ $RX -lt 1024 ];then
        RX="${RX}B/s"
      elif [ $RX -gt 1048576 ];then
        RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
      else
        RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
      fi
     
      if [ $TX -lt 1024 ];then
        TX="${TX}B/s"
      elif [[ $TX -gt 1048576 ]];then
        TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
      else
        TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
      fi
     
      echo -e "$ethn 	 $RX   $TX   			 `date +%k:%M:%S` "
     
    done
    

      

    修改后的文件

    #!/bin/bash
    #Modified by lifei4@datangmobile.cn
    echo ===DTmobile NetSpeedMonitor===
    sleep 1
    echo loading...
    sleep 1
    
    ethn=$1
     
    while true
    do
      RXpre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
      TXpre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
      sleep 1
      RXnext=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
      TXnext=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
     
      clear
      echo -e "			  RX 		   TX  			 TIME"
     
      RX=$((RXnext-RXpre))
      TX=$((TXnext-TXpre))
     
      if [ $RX -lt 1024 ];then
        RX="${RX}B/s"
      elif [ $RX -gt 1048576 ];then
        RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
      else
        RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
      fi
     
      if [ $TX -lt 1024 ];then
        TX="${TX}B/s"
      elif [[ $TX -gt 1048576 ]];then
        TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
      else
        TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
      fi
     
      echo -e "$ethn 	 $RX   $TX   			 `date +%k:%M:%S` "
     
    done
    

      

    原因为,在$取值的时候,括号里面只需要跟变量即可(变量可自行进行计算),不需要对括号内进行运算的变量在进行取值操作。

      RX=$((${RXnext}-${RXpre}))
      TX=$((${TXnext}-${TXpre}))
      
      修改后:
      
      RX=$((RXnext-RXpre))
      TX=$((TXnext-TXpre))  
    

      

    然后就没有然后了~~~

  • 相关阅读:
    批量修改文件的编码格式至UTF-8
    springboot搭建
    Redit集群搭建-Sentinel模式搭建
    Java并发编程:深入剖析ThreadLocal
    Hibernate常见问题 No row with the given identifier exists问题的解决办法及解决
    vector删除元素与清除内存空洞
    BZOJ 1003 [ZJOI2006]物流运输trans SPFA+DP
    Mybatis+Oracle批处理
    【日常学习】【线性DP】codevs1044 拦截导弹题解
    hdu5353 Average
  • 原文地址:https://www.cnblogs.com/phyger/p/9525469.html
Copyright © 2020-2023  润新知