I got a request to pass a list of ip's as an array in a bash script. For example:
./myscript.sh 192.168.0.1,192.168.0.10,192.168.0.15......
The ip's in the above parameter should properly populate the array present in the bash script. I would like it if anyone can show it in conjunction with the getopts utlity.
FYI - I'm fairly new to bash, so please be understanding......
First you need to remove the commas from your input, the sed
takes care of this.
Then you can create an array with just the var=()
syntax.
#! /bin/bash
no_commas=`echo $1 | sed -e 's/,/ /g'`
ip_array=($no_commas)
for addr in ${ip_array[@]}; do
echo "Address: $addr"
done
Which gives me:
$./bash_array.sh 192.168.1.33,192.168.2.3
Address: 192.168.1.33
Address: 192.168.2.3