BASH: How to retrieve multiple ip addresses for a host name and put them into a BASH array

BASH‘s read has the capability of stuffing an array (ip_addresses) with the tokenized STDIN. The three less than signs “<<<" tells BASH to redirect the stdout output of the following statement. [bash highlight_lines="9"]#!/bin/bash servers=( ) for server in ${servers[@]}; do # declare that variable ip_addresses is an array. not strictly needed but makes it a bit more readable IMHO declare -a ip_addresses read -a ip_addresses <<< $( host $server | awk ' $3 == "address" { print $4 } ' ) for ip in ${ip_addresses[@]}; do sudo route add -host ${ip} gw dev bnep0 done done[/bash] We can see the output of the script: [text] is is is is is is[/text]

Share Button