简体   繁体   中英

Pattern Match in a Bash Script

I have a bash script as below:

#!/bin/bash
sh ~/Softwares/apache/kafka/kafka_2.11-0.10.0.0/bin/kafka-run-class.sh kafka.admin.ConsumerGroupCommand --describe --group $1 --zookeeper $2

I call this script as below from my terminal:

kafka-describe my-kafka-consumer localhost:2181

I would like to now pass just a variable instead of the zookeeper address so that I do not have to remember the address of the zookeeper all the time. For example., I would like to be able to call the kafka-describe command as below:

kafka-describe my-kafka-consumer integration - would run against the integration environment

kafka-describe my-kafka-consumer uat - would run against the uat environment

I can then hard code the location of the zookeeper address in my script for the different environments. I'm totally new to writing bash scripts. Any suggestions on how to do this?

How about simple variables?

variables.sh

#!/usr/bin/bash

INTEGRATION="localhost:2080"
UAT="localhost:8080"

script.sh

#!/usr/bin/bash

# Imports variables from variables.sh file
source variables.sh

# "$VARIABLE" will give the value of the variable named VARIABLE
kafka-describe my-kafka-consumer "$UAT"

From what i understand, i guess below script will do your job:

#!/bin/bash
kafka_group=$1 #store the group in a variable 
kafka_env=$2 #store the env in another variable

if [ "$kafka_env" = "integration" ]; then
   addr="localhost:2080" #change to whatever value you require for integration
elif [ "$kafka_env" = "uat" ]; then
   addr="localhost:8080" #change to whatever value you require for uat
else
   echo "invalid input"
   exit 1
fi

sh ~/Softwares/apache/kafka/kafka_2.11-0.10.0.0/bin/kafka-run-class.sh kafka.admin.ConsumerGroupCommand --describe --group ${kafka_group} --zookeeper ${addr}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM