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)

请帮忙

4

1 回答 1

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
于 2013-03-15T08:16:30.373 回答