基本的bash脚本awk和一个除法 [英] Basic bash script awk and a division
问题描述
首先对这个基本问题感到抱歉.但是我不是程序员,需要一些帮助.
First sorry for this basic question. But i´m not a programmer and need some help.
我有一个命令可以给我传感器的温度.
I have a command that give me the temperature of a sensor.
示例:
root@machine:~ $ snmpwalk 172.69.4.25 -v 2c -c rocommunity .1.3.6.1.3.1.1.4
iso.3.6.1.3.1.1.4.1.2.5.116.101.109.112.49.1 = STRING: "31625"
还有一个很小的基本bashscript,只给我字符串的值.
And a small basic bashscript that give me only the value of the string.
#!/bin/bash
SLAVE="/sys/devices/w1_bus_master1/28-80000007e290/w1_slave"
OUTPUT=$(/bin/cat $SLAVE | /usr/bin/awk -F 't=' ' { printf $2 } ')
echo $OUTPUT
w1_slave文件如下
w1_slave file is as below
25 01 55 00 7f ff 0c 0c 08 : crc=08 YES
25 01 55 00 7f ff 0c 0c 08 t=31625
但这是一个温度,我需要以/1000除法的值. 31625真的是31.625的降级.
But it´s a temperature, and i need the value with a / 1000 division. 31625 realy are 31.625 degress.
如果我放
#!/bin/bash
SLAVE="/sys/devices/w1_bus_master1/28-80000007e290/w1_slave"
OUTPUT=$(/bin/cat $SLAVE | /usr/bin/awk -F 't=' ' { printf $2 / 1000 } ')
echo $OUTPUT
给我
root@machine:~ $ /opt/scripts/gettemp.sh
031.625
031.625是结果,但是我不知道为什么不只放31.625并删除第一个0,
031.625 is the result, but i don´t know why no put only 31.625 and remove the first 0,
我做错什么了吗? 有人可以帮我吗?
Are i doing something wrong? Could anybody Help me?
谢谢 最好的问候
推荐答案
如果问题是awk格式,请尝试以下操作:
If the problem is the awk formatting, try this:
#!/bin/bash
SLAVE="/sys/devices/w1_bus_master1/28-80000007e290/w1_slave"
OUTPUT=$(/bin/cat $SLAVE | grep -o "t=.*" | /usr/bin/awk -F 't=' ' { printf "%.3f",$2 / 1000 } ')
echo $OUTPUT
无论如何,正如@Ed Morton所说,默认情况下,awk默认情况下不会填充零.需要/sys/devices/w1_bus_master1/28-80000007e290/w1_slave文件的样本输出来澄清问题.
Anyway, as @Ed Morton said, by default awk does not pad with zero by default. It would be needed a sample output of the /sys/devices/w1_bus_master1/28-80000007e290/w1_slave file to clarify the question.
这篇关于基本的bash脚本awk和一个除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!