【发布时间】:2019-12-26 18:12:35
【问题描述】:
验证文件中是否存在基本机器 IP
尝试使用带有 if 条件的 for 循环来解决它
#!/bin/bash
#file with IP List /tmp/ip_list.txt
curr_ip="$(hostname -I)"
for n in `cat /tmp/ip_list.txt`;
do
if [ "$n" == "$curr_ip" ]; then
echo "success"
else
echo "fail"
fi
done
默认情况下是运行 else 条件。
【问题讨论】:
-
如果
ip_list.txt文件包含逐行数据,您最好使用grep来查找给定IP 是否出现在文件中。 -
/tmp/ip_list.txt的格式是什么?如果在for循环中回显出$n的值会怎样? -
它的IP格式如下:x.x.x.x x.x.x.x
-
请edit您的问题,而不是在评论中添加信息。要调试您的代码,您可以打印
$n和$curr_ip,最好在其前后添加一些特殊字符,例如echo "n <$n> curr_ip <$curr_ip>"