【问题标题】:Bash ping output in csv formatcsv 格式的 Bash ping 输出
【发布时间】: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 的例子会很好

标签: bash csv ping


【解决方案1】:

这是一个相当古老的问题,但我今天偶然发现了它。下面我粘贴了上述脚本的轻微修改版本,它修复了if 问题并适用于 Mac OS。

附:您可以取消注释 # prctg=100.0% 行以查看 if 工作。

#!/bin/bash

declare site=''
declare result=''
declare prctg=''

[[ "$1" == "www."* ]] && site="$1" || site="www.$1"

result="$site"

pingOutput=$(ping $site -c10 -i0.2 -q | tail -n2)

fl=true

while IFS= read -r line
do
  #echo $line
  if [ "$fl" == "true" ]
  then
    prctg=$(echo "$line" | grep -Eo "[.[:digit:]]{1,10}%")
    result="$result,$prctg"
    fl=false
   # prctg=100.0%
  else
    if [ "$prctg" == "100.0%" ]
    then
      result="$result,-1,-1,-1,-1"
    else
      result="$result,$(echo "$line" | cut -d' ' -f4 | sed -E 's/\//,/g')"
    fi
  fi
done <<< "$pingOutput"

echo "$result"

我希望它对未来的人有所帮助! :)

【讨论】:

    【解决方案2】:

    由于 pingOutput 的第二行从未被处理(循环之前结束),因此从未执行将 -1 添加到输出的操作。

    由于这个问题,我决定捕获失败的百分比并在没有返回数据包时采取行动 (100%),我还简化了您最初使用的一些表达式。

    我研究了脚本并想出了以下解决方案:

    #!/bin/bash
    
    declare site=''
    declare result=''
    declare prctg=''
    
    [[ "$1" == "www."* ]] && site="$1" || site="www.$1"
    
    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 !!!
    echo $line
    if [ "$fl" == "true" ]
        then
            prctg=$(echo "$line" | grep -Po "[0-9]{0,3}(?=%)")
            result="$result $prctg"
            fl=false
        fi
    if [ "$prctg" == "100" ]
        then
            result="$result -1 -1 -1 -1"
        else
            result="$result $(echo "$line" | cut -d' ' -f4 | sed -r 's/\// /g')"
        fi
    done <<< "$pingOutput"
    
    echo "$result"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多