执行ping命令时出错

时间:2016-06-16 09:42:05

标签: bash

host.txt:

www.google.com

test.sh:

#!/usr/bin/env bash

while IFS=$'\n' read -r line; do
    echo $line
    echo "#1"
    ping -c 1 $line
    line2="www.google.com"
    echo "#2"
    ping -c 1 $line2
done < $hostfile
exit 0

输出:

> test.sh
www.google.com
#1
ping: unknown host www.google.com
#2
PING www.google.com (74.125.206.147) 56(84) bytes of data.
64 bytes from wk-in-f147.1e100.net (74.125.206.147): icmp_seq=1 ttl=46 time=22.1 ms

--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 22.111/22.111/22.111/0.000 ms

有人能说出为什么第一次ping失败了吗?

感谢。

1 个答案:

答案 0 :(得分:2)

可能您的txt文件包含CRLF行结尾
和bash实际上试图ping www.google.com^M名称。