I want to generate a comma separated ip values with mapped ports and create a string.
Here is my code:
zk_ip="['192.168.0.10', '192.168.0.20', '192.168.0.30']" zk_host="" for i in $zk_ip[@] do add=$(echo "$i:2181") zk_host="$zk_host $add" done echo $zk_host Output:
[192.168.0.10,:2181 192.168.0.20, :2181 192.168.0.30]:2181 Expected ouptut:
192.168.0.10:2181, 192.168.0.20:2181, 192.168.0.30:2181
jsonmodule to make it JSON, and then you can process it in bash withjq). Is there a reason you don't define it aszk_ip=( 192.168.0.10 192.168.0.20 192.168.0.30 ), which is the native format for defining a bash array?zk_ip='["192.168.0.10", "192.168.0.20", "192.168.0.30"]'-- note the single quotes on the outside and the double quotes on the inside."${zk_ip[@]}"to iterate -- the curly braces are mandatory when you're using[@]).