shell自动化配置hadoop配置文件示例

#!/bin/bash
read -p 'Please input the directory of hadoop , ex: /usr/hadoop :' hadoop_dir
if [ -d $hadoop_dir ] ; then
   echo 'Yes , this directory exist.'
else 
   echo 'Error , this directory not exist.'
   exit 1
fi

if [ -f $hadoop_dir/conf/core-site.xml ];then
   echo "Now config the $hadoop_dir/conf/core-site.xml file."
   read -p 'Please input the ip value of fs.default.name , i.e. hdfs://ip:port :' ip
   i=1
   while [ -z $ip ]
   do
      read -p 'Please input the ip value of fs.default.name , i.e. hdfs://ip:port :' ip
      let i++ 
      echo $i
      if [ $i -gt 2 ];then
         echo 'You have input three time , done , exit.'
         exit 1
      fi
   done
   if [ $ip = '' ];then 
      echo 'The value of ip can not null , exit.'
      exit 1
   fi 
   read -p "Please input the port value of fs.default.name , i.e. hafs://$ip:port :" port
   if [ $port = '' ];then 
      echo 'The value of port can not null , exit.'
      exit 1
   fi
   read -p 'Please input the dir value of hadoop.tmp.dir :' hadoop_tmp_dir
   if [ $hadoop_tmp_dir = '' ];then
      echo 'The value of hadoop.tmp.dir can not null , exit.'
      exit 1
   else
      if [ ! -d $hadoop_tmp_dir ];then
         echo 'The directory you have input is not exist , we will make it.'
         mkdir -p $hadoop_tmp_dir
      fi
   fi
   tmp_dir=$(echo $hadoop_tmp_dir|sed 's:/:\\/:g')
      
   sed -i "s/ip/$ip/g" $hadoop_dir/conf/core-site.xml
   sed -i "s/port/$port/g" $hadoop_dir/conf/core-site.xml 
   sed -i "s/tmp_dir/$tmp_dir/g" $hadoop_dir/conf/core-site.xml
else 
   echo "The file $hadoop_dir/core-site.xml doen't exist."
   exit 1
fi
cat $hadoop_dir/conf/core-site.xml
echo 'Config the core-site.xml success !'
echo 

关键部分分析:

        1.   tmp_dir=$(echo $hadoop_tmp_dir|sed 's:/:\\/:g')

              我们输入的$hadoop_tmp_dir是类似:/usr/hadoop/tmp这样的,如果直接写在sed里:sed -i "s/tmp_dir/$hadoop_tmp_dir/g" $hadoop_dir/conf/core-site.xml ,这样会出现错误。因为它解析进去会是:sed -i "s/tmp_dir//usr/hadoop/tmp/g" $hadoop_dir/conf/core-site.xml这样的,显然不能满足,这里解决的方式是:想办法将/usr/hadoop/tmp的输入转换为:\ /usr\ /hadoop\/tmp的形式,即加入转义字符。sed ' s:/:\\/:g ' 这样就可以了,然后通过$(),取值赋给变量。

        2. sed -i " s/ip/$ip/g " $hadoop_dir/conf/core-site.xml    

             这里就是所谓的对文件中字符串的替换,示例文件为:            

root@ubuntu:/home/houqd/hadoop/conf# cat -n core-site.xml 
     1	<?xml version="1.0"?>
     2	<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
     3	
     4	<!-- Put site-specific property overrides in this file. -->
     5	
     6	<configuration>
     7		<property>
     8			<name>fs.default.name</name>
     9			<value>hdfs://ip:port</value>
    10		</property>
    11		<property>
    12			<name>hadoop.tmp.dir</name>
    13			<value>tmp_dir</value>
    14		</property>
    15	</configuration>

         3. 注意其它语法:   if [ ] ; then   else  fi          while [] do  ..done         i=0   let i++

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值