2016-07-26 68 views
0

我有如下一个bash脚本:模式匹配的bash脚本

#!/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 

我下面把这个脚本从我的终端:

kafka-describe my-kafka-consumer localhost:2181 

我想现在通过只是一个变量而不是动物园管理员的地址,这样我就不必一直记住动物园管理员的地址。例如,我想能够调用的卡夫卡描述命令如下:

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

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

我可以再硬编码在不同的环境中我的脚本动物园管理员地址的位置。我对编写bash脚本完全陌生。有关如何做到这一点的任何建议?

回答

1

从我的理解,我想下面的脚本将做您的工作:

#!/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} 
1

简单变量如何?

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"