我有一个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
我现在想传递一个变量而不是zookeeper地址,这样我就不必一直记住zookeeper的地址。例如,我希望能够调用kafka-describe命令,如下所示:
kafka-describe my-kafka-consumer integration - would run against the integration environment
kafka-describe my-kafka-consumer uat - would run against the uat environment
然后我可以在我的脚本中为不同的环境硬编码zookeeper地址的位置。我是编写bash脚本的新手。有关如何做到这一点的任何建议吗?
答案 0 :(得分: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"
答案 1 :(得分: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}