我正在尝试编写一个shell脚本,用作HDD温度警报任务的cron job。cron job必须执行以下操作:
我的实际代码是:
以上代码正确输出了"arrdiscs",但"TEMP"打印为空。那么我该如何基于TEMP变量创建一个数组,以便我可以验证哪个温度高于TEMP_MAX?
根据Etan Reisner的建议进行了一些更改:
但是我得到的结果如下:
变量temp_max被设置为20,但是没有显示任何“以下光盘过热”消息... :(
- 验证现有的ATA设备(我正在使用camcontrol);
- 验证已识别设备的温度(我正在使用smartctl);
- 将ATA设备的温度与常量变量进行比较;
- 如果其中一个设备的温度高于常量变量,则脚本发送一封电子邮件,其中包含所有ATA设备的温度。
我的实际代码是:
# Defines alert temperature
TEMP_MAX=30 #Graus celcius
# Create an array with devices
arrdiscs=($(camcontrol devlist | awk '{print substr($NF, 8, length($NF)-8)}'))
# Get the temperature of the devices
for i in "${arrdiscs[@]}"
do
# Get temperatures
TEMP="$(smartctl -A /dev/$i | egrep ^194 | awk '{print $10}')"
done
如果我使用以下代码,就可以输出设备名称和温度:
for i in "${arrdiscs[@]}"
do
TEMP="$(smartctl -A /dev/$i | egrep ^194 | awk '{print $10}')"
echo "[$i]: $TEMP"
done
我尝试了很多方法(和许多其他人一样):
for i in "${arrdiscs[@]}"
do
TEMP="$(smartctl -A /dev/$i | egrep ^194 | awk '{print $10}')"
echo "[$i]: $TEMP"
done
echo "ARRAY DISCOS: ${arrdiscs[*]}"
echo "TEMPERATURAS: ${TEMP[*]}"
以上代码正确输出了"arrdiscs",但"TEMP"打印为空。那么我该如何基于TEMP变量创建一个数组,以便我可以验证哪个温度高于TEMP_MAX?
根据Etan Reisner的建议进行了一些更改:
# Defines alert temperature
temp_max=20 #Graus celcius
# Monta array de discos do sistema
arrdiscs=($(camcontrol devlist | awk '{print substr($NF, 8, length($NF)-8)}'))
echo "${arrdiscs[@]}"
alertdiscs=()
for i in "${arrdiscs[@]}"
do
temp=("$(smartctl -A "/dev/$i" | egrep ^194 | awk '{print $10}')")
if [ -z "$temp" ]; then
echo "\$temp is empty"
else
if [ $temp -gt $temp_max ]; then
alertdiscs+=("[$i]: $temp")
echo ${temp[@]}
fi
fi
done
if [ ${#alertdics[@]} -gt 0 ]; then
echo "The following discs were too hot:"
printf '%s\n' "${alertdiscs[@]}"
else
echo "${alertdiscs[@]}"
fi
但是我得到的结果如下:
ada0 ada1 ada2 ada3 da0
33
31
32
30
$temp is empty
[ada0]: 33 [ada1]: 31 [ada2]: 32 [ada3]: 30
变量temp_max被设置为20,但是没有显示任何“以下光盘过热”消息... :(