批量添加主机名映射关系问题

来源:互联网 发布:腾讯购物软件 编辑:程序博客网 时间:2024/06/11 08:46

因为 tomcat 在启动的时候需要在 /etc/hosts 里面添加下主机名的映射关系,但是如果服务器多的话,手工一台台去填写就不现实了,所以就用脚本批量处理下。

正确的版本:

HOSTNAME=`hostname`ip_addr=`/sbin/ifconfig bond0 |grep -a "inet addr:" |awk -F":" '{print $2}' |egrep -o '([0-9]{1,3}\.?){4}'`echo ${ip_addr} > temp.txtsudo sh -c 'echo "`cat temp.txt` ${HOSTNAME}" >> /etc/hosts'

有问题的版本:

#!/bin/bashHOSTNAME=`hostname`ip_addr=`/sbin/ifconfig bond0 |grep -a "inet addr:" |awk -F":" '{print $2}' |egrep -o '([0-9]{1,3}\.?){4}'`sudo sh -c 'echo "${ip_addr} ${HOSTNAME}" >> /etc/hosts'

有问题的版本输出的结果是,只会在 /etc/hosts 文件中输入主机名以及一个空格,想不明白,那个 ip_addr 变量绝对是有值的。

0 0
原创粉丝点击