以下脚本正在生成所需的输出,但未将结果重定向到/home/myuser/slavedelay.html
#!/斌/庆典
host=<ip>
echo $host
user=usr1
password=mypass
threshold=300
statusok=OK
statuscritical=CRITICAL
for i in ert7 ert9
do
echo "<html>" > /home/myuser/slavedelay.html
if [ "$i" == "ert7" ]; then
slvdelay=`mysql -u$user -p$password -h<ip> -S /backup/mysql/mysql.sock -e 'show slave status\G' | grep Seconds_Behind_Master | sed -e 's/ *Seconds_Behind_Master: //'`
if [ $slvdelay -ge $threshold ]; then
echo "<tr><td>$i</td><td>CRITICAL</td>" >> /home/myuser/slavedelay.html
echo "<tr><td>$i</td><td>CRITICAL</td>"
else
echo "<tr><td>$i</td><td>OK</td>" >> /home/myuser/slavedelay.html
echo "<tr><td>$i</td><td>OK</td>"
fi
fi
done
echo "</html>" >> /home/myuser/slavedelay.html
如果我将输出文件/home/myuser/slavedelay.html告知它。
<html>
</html>
执行结果:
sh slave_delay.sh
<tr><td>sdb7</td><td>OK</td>
答案 0 :(得分:2)
每次循环都会因为命令
而清空输出文件echo "<html>" > /home/myuser/slavedelay.html
因此,第一次迭代将<tr>
行写入文件,然后下一次迭代将覆盖该文件,并且不会写入这些行,因为$i
不是ert7
。
将其更改为:
for i in ert7 ert9
do
if [ "$i" == "ert7" ]; then
echo "<html>" > /home/myuser/slavedelay.html
slvdelay=`mysql -u$user -p$password -h<ip> -S /backup/mysql/mysql.sock -e 'show slave status\G' | grep Seconds_Behind_Master | sed -e 's/ *Seconds_Behind_Master: //'`
if [ $slvdelay -ge $threshold ]; then
echo "<tr><td>$i</td><td>CRITICAL</td>"
else
echo "<tr><td>$i</td><td>OK</td>"
fi | tee -a /home/myuser/slavedelay.html
echo "</html>" >> /home/myuser/slavedelay.html
fi
done
答案 1 :(得分:0)
替换:
if [ "$i" == "ert7" ];
使用:
if [ "$i" = "ert7" ];
您也可以在测试中使用=
运算符。