1、host文件原内容
cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.21 db redis
192.168.10.22 es
192.168.10.23 spark kafka zookeeper
192.168.10.24 tomcat flume
2、去掉localhost的列
cat /etc/hosts|grep -vE localhost
192.168.10.21 db redis
192.168.10.22 es
192.168.10.23 spark kafka zookeeper
192.168.10.24 tomcat flume
3、截取IP(cut)以空格分割,取分割后第一个字段
cat /etc/hosts|grep -vE localhost | cut -d " " -f 1
192.168.10.21
192.168.10.22
192.168.10.23
192.168.10.24
在shell脚本中放入数组操作:
#!/bin/bash
ip_list=`cat /etc/hosts|grep -vE "localhost"|cut -d " " -f 1`
for ip in $ip_list;do
echo $ip
done