首页 > 解决方案 > 在while循环外读取变量

问题描述

我想在while..循环之外读取一个IP(变量)来定位它们。

问题是将结果存储在“STORE_LOCATION”中,然后在循环后调用它:

 STORE_LOCATION=''

 echo -e "$SUS_IP" | "$AWK" '{ print $2 }' |\

 while read -r IP
 do
    #echo "IP $IP"
    STORE_LOCATION+=$( "$CURL" https://ipinfo.io/"$IP" )

 done

 echo "$STORE_LOCATION"

预期结果是:

 {
 "ip": "XXX.XXX.XXX.XXX",
 "city": "Seattle",
 "region": "Washington",
 "country": "US",
 "loc": "47,-11",
 "org": "Inc.",
 "postal": "889988",
 "timezone": "America/Los_Angeles",
 "readme": "https://ipinfo.io/missingauth"
}


{
"ip": "YYY.YYY.YYY.YYY",
  "hostname": "HOST",
  "city": "Roubaix",
  "region": "Hauts-de-France",
  "country": "FR",
  "loc": "50,3",
  "org": "SAS",
  "postal": "59100",
  "timezone": "Europe/Paris",
  "readme": "https://ipinfo.io/missingauth"
}

提前致谢

标签: bashloopswhile-loop

解决方案


推荐阅读