I have below script that I get details of some IPs in a blacklist.
check.sh
blacklist_ips='
5.56.148.140
94.73.159.66
113.171.224.169
107.150.42.226
195.159.233.44
89.19.7.58
'
for ipx in $blacklist_ips
do
country=`whois $ipx | grep -i country | tail -1 | awk '{print $2}'`
hostx=`host $ipx |awk '{print $NF}'|sed "s/\.$//"`
printf '%s %s %s' $country $ipx $hostx
printf '\n'
done
It works but I want to sort the output by country section.
Now I can simply go:
bash check.sh | sort -nr
and it works OK.
But I want to do the sorting before sending the result to terminal. In other words sorting should be done inside check.sh and then should be sent to terminal.
How can I achieve this in bash script?
printf '%s %s %s\n' $country $ipx $hostx > file.txtthen addsort -nr file.txtafter the for loop?