我正在尝试通过bash脚本创建Kafka主题的json,该脚本稍后可用于重新分配分区。
当我创建主题数组时,我成功获取了JSON,该主题数组来自使用新行作为IFS的文件。但是,当我尝试从命令的输出创建数组时,它给出了不同的JSON。
#!/bin/bash -xe
topics=( $(docker run -it confluentinc/cp-kafka bash -c "kafka-topics --zookeeper kafka-0:2181 --list") )
printf "%s\n" "${topics[@]}"
echo -e "{\"topics\":["
printf '{"topic": "%s"},\n' "${topics[@]}" | sed '$s/,$//'
echo -e "],"
echo -e "\"version\":1"
echo -e "}"
输出:
__confluent.support.metrics
__consumer_offsets
example1
example2
example3
example4
example5
{"topics":[
"},opic": "__confluent.support.metrics
"},opic": "__consumer_offsets
"},opic": "example1
"},opic": "example2
"},opic": "example3
"},opic": "example4
"}topic": "example5
],
"version":1
}