每次安装Hadoop的时候都需要配置环境变量,写个脚本记着下次速度刚刚的。(从一个安装jdk的版本修修补补出来的)
脚本和参数说明脚本
#!/bin/sh
###检查是否是 管理员账户root
[ $(id -u) != "0" ] && { echo "***Error: You must be root to run this script"; exit 1; }
###默认安装路径
install_dir="/my/hadoop"
echo ${var%/*}
###读取文件名称
install_file_name=$2
install_folder_name=${install_file_name%.tar*}
###查询当前路径
current_dir="$(pwd)"
exit_ok=0
exit_fail=1
if [ -z "$1" ]
then
echo "use default install dir:$install_dir"
else
echo "use input param install_dir:$1"
install_dir=$1
fi
###检查目录是否存在不存在则安装
if [ ! -d $install_dir ]; then
mkdir -p $install_dir || exit $exit_fail
fi
hadoop_install_dir=$install_dir/$install_folder_name
if [ ! -d $hadoop_install_dir ]; then
mkdir -p $hadoop_install_dir || exit $exit_fail
fi
###检查安装目录下是否已经存在其他版本 已经安装就退出 ---------------------
if [ -f $hadoop_install_dir/bin/hadoop ]; then
echo "****notice*** $hadoop_install_dir/bin/hadoop found, nothing will install"
echo "****notice*** if you really want to install , please rm -fr $hadoop_install_dir manually first,then run again"
echo "env HADOOP_HOME:$hadoop_install_dir"
exit $exit_fail
fi
###解压安装文件
tar -zxvf ${install_file_name} -C $install_dir
#自动配置系统变量
if [ -z `cat /etc/profile|grep 'HADOOP_HOME='` ]; then
echo " " >> /etc/profile
echo "##HADOOP_HOME " >> /etc/profile
echo "export HADOOP_HOME=$hadoop_install_dir" >> /etc/profile
echo 'export PATH=$PATH:$HADOOP_HOME/bin' >> /etc/profile
echo 'export CLASSPATH=.:$HADOOP_HOME/sbin' >> /etc/profile
else
echo `cat /etc/profile|grep 'HADOOP_HOME='`
echo `cat /etc/profile|grep 'HADOOP_HOME/bin'`
echo `cat /etc/profile|grep 'HADOOP_HOME/lib'`
fi
#source /etc/profile
echo "success install hadoop, please use 'source /etc/profile' to enable"
运行说明
./install.sh /my/hadoop/ hadoop-3.3.0.tar.gz
./install.sh {安装的目录} {Hadoop的文件(必须是当前目录)}
一个常见问题说明如果运行中出现了“/bin/sh^M: bad interpreter”的错误大概率是因为编码问题解决方法
.sh文件的格式为dos格式。而linux只能执行格式为unix格式的脚本
可以使用vi命令进行修改,步骤如下:
- 使用vi命令打开文件
- 查看编码 输入: set ff
-
:set ff
- 如果不是unix就需要修改成unix 输入:set ff=unix
-
: set ff=unix



