我不希望数组进行单词拆分

时间:2013-03-15 07:05:37

标签: bash

以下是我的代码。但是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)

请帮忙

1 个答案:

答案 0 :(得分:0)

据我所知,您的阵列分为以下几种:

物理

ID

0

等等。要更改此行为,您需要将内部字段分隔符(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