I have a for loop as shown below. I need the whole output of the loop added into an array or variable.
for i in $ip
do
curl -s $i:9200
done
Any idea how I can achieve that?
All arrays are the contiguous block of memory locations. By default, the lowest position of the array stores the first element, and the highest position stored the last data. In C, the array is declared by specifying the element's type and the total length of array required to store the data.
For Loop to Traverse Arrays. We can use iteration with a for loop to visit each element of an array. This is called traversing the array. Just start the index at 0 and loop while the index is less than the length of the array.
You can use it like this:
# declare an array
declare -a arr=()
for i in $ip
do
# append each curl output into our array
arr+=( "$(curl -s $i:9200)" )
done
# check array content
declare -p arr
It is important to use quotes around curl comment to avoid splitting words of curl
command's output into multiple array entries. With quotes all of the curl output will become a single entry in the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With