验证文件中是否存在

验证文件中是否存在

This question already has answers here:
How to test if string exists in file with Bash?
                                
                                    (12个答案)
                                
                        
                                7个月前关闭。
            
                    
验证文件中是否存在基本计算机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


默认情况下,它正在运行其他条件。

最佳答案

#!/bin/bash

curr_ip="$(hostname -I)"

output=$(grep -c /tmp/ip_list.txt)

if [ "$output" != "0" ]
then
   echo "success"
else
   echo "failure"
fi


grep中的-c选项为您计数。如果不等于零,则发现了一些东西。

关于linux - 验证文件中是否存在IP地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57592068/

10-13 07:58