简体   繁体   中英

How to input endlessly by shell script

I am trying to use shell script to generate data to my Kafka topic.

Firstly, I write a shell script 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"}]}' 

It requires you to input string like "key1":{"measurement": "INFO", "id": 1} in command line when the run_producer.sh is executed, and you can input as many as you want.

I write another script add_data.sh :

#!/bin/sh

s="\"key1\":{\"measurement\": \"INFO\", \"id\": 1}"
printf "${s}\n${s}\n" | ./run_producer.sh

It can input the string 2 times, or more by adding "${s}\\n" in printf , but it is limited and stupid.

I want to make it inputs the string endlessly until I stop it. How can I do that with shell script ?

I will be very grateful if you can tell me how to make the string differently(different data) by the way.

You could use yes "$s" to produce endless input for your script. But what do you meen by "make the string differently"? Will be enough to use infinite loop with some random data, like

while true; do s="\"key1\":{\"measurement\": \"INFO\", \"id\": $RANDOM}"; echo $s; done

or you need modify it in some other way?

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.

You could combine it with anything else, like: "key_${RANDOM}" Or choose any other method like https://gist.github.com/earthgecko/3089509 or https://unix.stackexchange.com/questions/230673/how-to-generate-a-random-string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM