我正在尝试使用shell脚本来生成我的Kafka主题的数据。
首先,我编写了一个外壳脚本run_producer.sh
#!/bin/sh
./bin/kafka-avro-console-producer --broker-list localhost:9092 --topic AAATest2 \
--property "parse.key=true" \
--property "key.separator=:" \
--property key.schema='{"type":"string"}' \
--property value.schema='{"type":"record","name":"myrecord","fields":[{"name":"measurement","type":"string"},{"name":"id","type":"int"}]}' 它要求您在执行run_producer.sh时在命令行中输入类似"key1":{"measurement": "INFO", "id": 1}的字符串,并且您可以根据需要输入任意数量的字符串。
我写了另一个脚本add_data.sh
#!/bin/sh
s="\"key1\":{\"measurement\": \"INFO\", \"id\": 1}"
printf "${s}\n${s}\n" | ./run_producer.sh它可以输入字符串2次,或者通过在printf中添加"${s}\n“来输入更多,但它是有限的和愚蠢的。
我想让它无休止地输入字符串,直到我停止它。我如何使用shell脚本做到这一点?
如果你能告诉我如何使字符串不同(不同的数据),我将非常感激。
发布于 2019-03-23 18:41:50
您可以使用yes "$s"为您的脚本生成无穷无尽的输入。但是,你所说的“以不同的方式制作字符串”是什么意思呢?将足以对一些随机数据使用无限循环,例如
while true; do s="\"key1\":{\"measurement\": \"INFO\", \"id\": $RANDOM}"; echo $s; done或者您需要以其他方式修改它?
man bash
RANDOM Each time this parameter is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset.您可以将其与任何其他方法组合使用,如:"key_${RANDOM}"或选择任何其他方法,如https://gist.github.com/earthgecko/3089509或https://unix.stackexchange.com/questions/230673/how-to-generate-a-random-string
https://stackoverflow.com/questions/55312802
复制相似问题