2016-07-26 149 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"