我尝试编写一些 bash 代码来编写温度日志。在记录文件中写入 100 个时间点和测量点,然后将数据移动到临时文件中,并且应该重新开始记录到原始文件中。临时文件每 100 秒被删除一次,因为我只需要最后几分钟的记录并且想要防止垃圾。
除此之外,代码可能看起来不必要地复杂(我是初学者) - 错误在哪里?我预计计数器(打印只是为了看看发生了什么)会计数到 100,但它只打印出来:
1
2
它只将两个时间点而不是 100 个写入文件。这是代码:
#!/bin/bash
COUNTER=0
#Initial temporary file is created
echo '' > temperaturelogtemporary.txt;
#100 timepoints are written into temperaturelog.txt
while true; do
echo `date` '->' `acpi -t`>> temperaturelog.txt;
sleep 1;
#as soon as 100 timepoints are recorded...
if [[ $COUNTER > 100 ]];
then
#...the old temporary file is removed and
#the last records are renamed into a new temporary file
rm temperaturelogtemporary.txt;
mv temperaturelog.txt temperaturelogtemporary.txt;
COUNTER=0;
fi
COUNTER=$(($COUNTER + 1));
echo $COUNTER;
done