shell自動化配置hadoop設定檔樣本

來源:互聯網
上載者:User
#!/bin/bashread -p 'Please input the directory of hadoop , ex: /usr/hadoop :' hadoop_dirif [ -d $hadoop_dir ] ; then   echo 'Yes , this directory exist.'else    echo 'Error , this directory not exist.'   exit 1fiif [ -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.xmlelse    echo "The file $hadoop_dir/core-site.xml doen't exist."   exit 1ficat $hadoop_dir/conf/core-site.xmlecho '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++

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.