【发布时间】:2015-11-15 13:23:03
【问题描述】:
我的目标是将 ping 命令的输出(最后两行)转换为 CSV 样式。
这里有一些例子:
如果丢包率低于100%
URL, PacketLoss, Min, Average, Max, Deviation
如果丢包率等于 100%
URL, 100, -1, -1, -1, -1
我的脚本在下面,但是当丢包率为 100% 时输出是:
URL, 100,
所以问题出在 if 语句上,因为它没有进入 elif,所以我使用与检查地址是否已满相同的语法(是否带有“www.”)。
请您看一下,因为我尝试了多种方法,但都没有成功。
我的脚本:
#!/bin/bash
declare site=''
declare result='';
if [[ "$1" == "www."* ]]; then
site="$1";
else
site="www.$1";
fi
result="$site";
pingOutput=$(ping $site -c10 -i0.2 -q| tail -n2);
fl=true;
while IFS= read -r line
do
# !!! The problem is here, the if statement is not working properly and I do not know why !!!
if [ "$fl" == "true" ]; then
result="$result $(echo "$line" | cut -d',' -f3 | cut -d" " -f2 | sed -r 's/%//g')";
fl=false;
elif [[ "$line" == "ms"* ]]; then
result="$result $(echo "$line" | cut -d' ' -f4 | sed -r 's/\// /g')";
else
result="$result -1 -1 -1 -1";
fi
done <<< "$pingOutput"
echo "$result";
【问题讨论】:
-
感谢 @hrbrmstr 编辑帖子,我对 Stackoverflow 还是很陌生。
-
如果你添加 pingOutput 的例子会很好