FreeSoftwareServers
FreeSoftwareServers

Reputation: 2791

Grep IP addresses into Array and Parse Output

I have a script which greps a bunch of IP's. I want to parse the results into an array:

For i in IP
    echo IP
done

But as it stands now, I get the same IP over and over.

My code:

#!/bin/bash

IP=($(showmount -a | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"))

ARRAY="$(showmount -a | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")"
echo "$ARRAY"
echo
for i in "${IP[@]}"
do
echo $IP
done

And when I run it I get:

root@venom:~# nano test.sh && ./test.sh
10.1.10.10
10.1.10.11
10.1.10.129
10.1.10.130
10.1.10.13
10.1.11.73

10.1.10.10
10.1.10.10
10.1.10.10
10.1.10.10
10.1.10.10
10.1.10.10

Upvotes: 2

Views: 387

Answers (1)

choroba
choroba

Reputation: 241868

Try outputting the loop variable instead of the first element of the array

for i in "${IP[@]}" ; do
    echo $i  # Not $IP.  $IP is the same as ${IP[0]}
done

Upvotes: 4

Related Questions