我有一个 bash 脚本,可以从文件 ping 主机,数据输出为
Ping 1 - some-ip - ... which
Ping 2 - some-ip- ...successful
Ping 3 - some-ip - ...failure
Ping 4 - some-ip - ... failure
Ping 5 - some-ip - ...successful
Ping 6 - some-ip - ... failure
我该如何做才能只输出失败?
我的脚本如下:
#!/bin/bash
hosts=/path/to/file
echo while IFS=';' read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 || [ -n "$f2" ] ; do
echo -e -n "Pinging wg-ip $f1 - $f2 - ..."
ping -f -W 2 -c 1 "$f2" &>/dev/null && echo success || echo fail
echo -e -n "Pinging white-ip $f1 - $f4 - ..."
ping -f -W 2 -c 1 "$f4" &>/dev/null && echo success || echo fail
done < <(tail -n +2 $hosts)
答案1
while IFS=';' read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 || [ -n "$f2" ] ; do
echo -e -n "Pinging wg-ip $f1 - $f2 - ..."
ping -f -W 2 -c 1 "$f2" &>/dev/null && echo success || echo fail
echo -e -n "Pinging white-ip $f1 - $f4 - ..."
ping -f -W 2 -c 1 "$f4" &>/dev/null && echo success || echo fail
done < <(tail -n +2 $centers) | grep fail