2013-03-15 65 views
0

下面是我的代码。但bash正在分词,因此我失败了。如何使我的脚本,以便没有分词。我不想让数组做分词

namaSensor=$(sensors | egrep "°C" | awk '{print $0}' | awk -F ':' '{print $1}') 
for sensor in $namaSensor 
do 
    if [ $(sensors | grep -c "$sensor") -ne 0 ] 
    then 
     currentTemperature=$(sensors | egrep "$sensor" | awk '{print $0}' | awk -F '+' '{print $2}' | cut -c 1-4 | awk -F '.' '{print $1}') 
     maxTemperature=$(sensors | egrep "$sensor" | awk '{print $0}' | awk -F '+' '{print $3}' | cut -c 1-4 | awk -F '.' '{print $1}') 
     if [ $currentTemperature -lt $maxTemperature ] 
     then 
      printf "current temperature is %d°C and the maximum allowed temperature is %d°C\n" "$currentTemperature" "$maxTemperature" 
      printf "temperature is within the maximum allowed temperature\n" 
      echo "$sensor" 
     else 
      printf "current temperature is %d°C and the maximum allowed temperature is %d°C\n" "$currentTemperature" "$maxTemperature" 
      printf "temperature is more than the maximum allowed temperature\n" 
      #exit 255 
     fi 
    fi 
done. 

这是我单位的传感器输出。

acpitz-virtual-0 
Adapter: Virtual device 
temp1:  +40.0°C (crit = +111.0°C) 
temp2:  +40.0°C (crit = +111.0°C) 

coretemp-isa-0000 
Adapter: ISA adapter 
Physical id 0: +34.0°C (high = +87.0°C, crit = +105.0°C) 
Core 0:   +31.0°C (high = +87.0°C, crit = +105.0°C) 
Core 1:   +22.0°C (high = +87.0°C, crit = +105.0°C) 

请帮

+0

我认为你还需要提供你期望看到的。此外,正在执行的传感器的输出将有助于 – Vahid 2013-03-15 07:10:14

+0

因此,如果同时存在最大温度和临界温度,则使用最大值。如果只有临界温度,那么应该使用? – Scrutinizer 2013-03-15 09:28:22

回答

0

据我了解你有你的阵列分裂如下:

物理

ID

等。要更改此行为,您需要将您的内部字段分隔符(IFS)更改为换行符。所以你的代码应该是这样的:

IFS=$'\n' 
nameSensor=$(sensors | egrep "°C" | awk '{print $0}' | awk -F ':' '{print $1}') 
for sensor in $nameSensor 
do 
    if [ $(sensors | grep -c "$sensor") -ne 0 ]; then 
     currentTemperature=$(sensors | egrep "$sensor" | awk '{print $0}' | awk -F '+' '{print $2}' | cut -c 1-4 | awk -F '.' '{print $1}') 
     maxTemperature=$(sensors | egrep "$sensor" | awk '{print $0}' | awk -F '+' '{print $3}' | cut -c 1-4 | awk -F '.' '{print $1}') 
     if [ $currentTemperature -lt $maxTemperature ]; then 
      printf "current temperature is %d°C and the maximum allowed temperature is %d°C\n" "$currentTemperature" "$maxTemperature" 
      printf "temperature is within the maximum allowed temperature\n" 
      echo "$sensor" 
     else 
      printf "current temperature is %d°C and the maximum allowed temperature is %d°C\n" "$currentTemperature" "$maxTemperature" 
      printf "temperature is more than the maximum allowed temperature\n" 
      #exit 255 
     fi 
    fi 
done 
+0

谢谢。它以我想要的方式工作。 – 2013-03-15 09:45:22