[英]Mutli-line AWK output to array
I am having some issues with a script that I am writing. 我正在编写的脚本存在一些问题。 Basically I want to retrieve a list of active IP address and add them into an array. 基本上,我想检索活动IP地址的列表并将其添加到阵列中。 But unfortunately after looking everywhere I haven't quite found a solution. 但是不幸的是,到处看我都还没有找到解决方案。
So far I have this: 到目前为止,我有这个:
#!/bin/bash
ipnet="192.168.0.0/24"
tail /proc/uptime | grep -o '^\S*'
Iparray=($(nmap -n -sn $ipnet -oG - | awk '/Up$/{print $2}'))
echo $Iparray[3]
echo "Active IPs:"
for i in "${Iparray[@]}"
do
:
echo $Iparray[$i]
done
But instead of the expected output being a list of IP's, I get this: 但是我得到的不是预期的输出是IP列表:
16977.41
192.168.0.1[3]
Active IPs:
192.168.0.1[192.168.0.1]
192.168.0.1[192.168.0.2]
192.168.0.1[192.168.0.3]
192.168.0.1[192.168.0.4]
...
192.168.0.1[192.168.0.254]
Not really quite sure what is happening... Any help would be appreciated. 不太确定发生了什么...任何帮助将不胜感激。
Very tiny issue: 非常小的问题:
The ${Iparray[@]}
is the list of the IP addresses, so you could: ${Iparray[@]}
是IP地址的列表,因此您可以:
for ip in ${Iparray[@]}; do
echo $ip
done
OR 要么
If you really want to use indexes: 如果您确实要使用索引:
for (( i=0; i<${#Iparray[@]}; i++ )) ; do
echo ${Iparray[$i]}
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.