0

I would like to read an IP (variable) outside while.. loop to locate them.

The thing is to store result in "STORE_LOCATION" then call it after the loop:

 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"

Expected result is:

 {
 "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"
}

Thanks in advance

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 1
    The problem is that your while-loop is run in a subshell, try `while read -r IP; do ...; done < <(echo -e "$SUS_IP" | "$AWK" '{ print $2 }')` – Freddy Apr 24 '20 at 22:36
  • Right, and if you could show the actual data that comes from `$SUS_IP` then someone can point you to avoid the `awk` call. – Jetchisel Apr 24 '20 at 22:40
  • Thanks @Freddy: There is an error: `syntax error near unexpected token <'` – Immo Broker Apr 24 '20 at 22:44
  • 1
    Use `bash` not `sh` – Jetchisel Apr 24 '20 at 22:45
  • @Jetchisel: SUS_IP: is a result of ips and their count: `2 AAA.AAA.AAA.AAA 18 BBB.BBB.BBB.BBB 43 CCC.CCC.CCC.CCC` – Immo Broker Apr 24 '20 at 22:46
  • Please add that to your post/question. – Jetchisel Apr 24 '20 at 22:48
  • @Jetchisel: Thanks I found the solution – Immo Broker Apr 24 '20 at 22:52
  • Then `while read _ ip; do echo "$ip"; done < file.txt` should show you the ip's only, anyways good luck. – Jetchisel Apr 24 '20 at 22:57
  • [Please don't add "solved" to your title or question](https://meta.stackexchange.com/a/116105/248627). Instead, [mark an answer correct by clicking on the checkmark](https://meta.stackexchange.com/q/5234/248627). You can also [add your own answer](https://stackoverflow.com/help/self-answer) and accept it if none of the ones you received solved your problem. – ChrisGPT was on strike Apr 25 '20 at 00:59

0 Answers0