Example of the hadoop configuration file automatically configured by Shell

Source: Internet
Author: User
Tags xsl
#!/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 

Key Analysis:

1. tmp_dir = $ (echo $ hadoop_tmp_dir | SED's:/: \/: G ')

The $ hadoop_tmp_dir we entered is similar to:/usr/hadoop/tmp. If it is written directly in sed: sed-I "s/tmp_dir/$ hadoop_tmp_dir/g" $ hadoop_dir/CONF/core-site.xml, which causes an error. Because it will be parsed into: sed-I "s/tmp_dir/usr/hadoop/tmp/g" $ hadoop_dir/CONF/core-site.xml, apparently not satisfied, the solution is to convert the/usr/hadoop/tmp input to \/usr \/hadoop \/tmp, that is, add escape characters. Sed's:/: \/: G'. Then, use $ () to assign the value to the variable.

2. Sed-I "s/IP/$ IP/g" $ hadoop_dir/CONF/core-site.xml

Here is the replacement of strings in the file. The example file is:

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. Pay attention to other syntaxes: If []; then else fi while [] Do... done I = 0 let I ++

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.