Grep IP地址进入数组并解析输出

时间:2021-07-13 14:02:26

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

我有一个脚本,里面有很多IP。我想把结果解析成一个数组:

For i in IP
    echo IP
done

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

但是现在,我一遍又一遍地得到相同的IP。

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

1 个解决方案

#1


4  

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

#1


4  

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