shell案例之一键部署kafka

[root@localhost shell]# cat 1.sh

#!/bin/bash

installDir="/opt/module/kafka"

kafka_version="1.1.1"

scala_version="2.11"

#检查Java环境

if [ -z "$JAVA_HOME" ] || [ ! -f "$JAVA_HOME/bin/java" ]; then

  echo "JAVA_HOME未设置或无效,请安装JDK 1.8并设置Java环境变量再来执行此脚本"

  exit 1

fi

#检查并创建安装目录

if [ ! -d "${installDir}" ]; then

  sudo mkdir -p "${installDir}"

  if [ $? -eq 0 ]; then

    echo "安装目录${installDir}已创建"

  else

    echo "请确保您有足够的权限来创建目录,请增加权限后再次执行"

    exit 1

  fi

fi

#下载kafka压缩包

if [ ! -f /tmp/kafka_$scala_version-$kafka_version.tgz ]; then

  wget https://downloads.apache.org/kafka/$kafka_version/kafka_$scala_version-$kafka_version.tgz -P /tmp/

  if [ $? -eq 0 ]; then

    echo "kafka_$scala_version-$kafka_version.tgz下载成功"

  else

    echo "kafka_$scala_version-$kafka_version.tgz下载失败,请重试或手动下载到/tmp目录下再次执行"

    echo "下载地址:https://archive.apache.org/dist/kafka/$kafka_version/kafka_$scala_version-$kafka_version.tgz"

    exit 1

  fi

fi

#解压kafka压缩包

tar -zxvf /tmp/kafka_$scala_version-$kafka_version.tgz -C $installDir

if [ $? -eq 0 ]; then

  echo "/tmp/kafka_$scala_version-$kafka_version.tgz解压成功"

else

  echo "/tmp/kafka_$scala_version-$kafka_version.tgz解压失败,请查看异常信息后重试"

  exit 1

fi

#设置kafka环境变量

if [ -z "$KAFKA_HOME" ]; then

  echo >> ~/.bashrc

  echo '#KAFKA_HOME' >> ~/.bashrc

  echo "export KAFKA_HOME=$installDir/kafka_$scala_version-$kafka_version" >> ~/.bashrc

  echo 'export PATH=$PATH:$KAFKA_HOME/bin' >> ~/.bashrc

else

  echo "KAFKA_HOME已有设置:$KAFKA_HOME"

Fi

# 备份原始配置文件

cp "$installDir/kafka_2.11-1.1.1/config/server.properties" "$installDir/kafka_2.11-1.1.1/config/server.properties.bak"

#修改kafka配置文件

ip_addr=$(ip addr | grep 'inet ' | awk '{print $2}'| tail -n 1 | grep -oP '\d+\.\d+\.\d+\.\d+')

sed -i "s|#listeners=PLAINTEXT://:9092|listeners=PLAINTEXT://$ip_addr:9092|" "$installDir/kafka_$scala_version-$kafka_version/config/server.properties"

if [ $? -eq 0 ]; then

  echo "kafka配置文件修改成功"

else

  echo "kafka配置文件修改失败,请查看异常信息后重试"

  exit 1

fi

echo "kafka下载、安装、配置成功"

exit 0

上一篇:创建包含可导入浏览器信任的SSL自签名证书


下一篇:C++反汇编逆向还原——数组与循环还原练习(笔记)