0

Data in log file is something like:

"ipHostNumber: 127.0.0.1
ipHostNumber: 127.0.0.2
ipHostNumber: 127.0.0.3"

that's my code snippet:

readarray -t iparray < "$destlog"
unset iparray[0]

for ip in "${iparray[@]}"
do
   IFS=$'\n' read -r -a iparraychanged <<< "${iparray[@]}"
done

And what I wanted to receive is to transfer IP's to another array and then read every line from that array and ping it.

UPDATE: I've acknowledged something, it's probably that I want one array to another but without some string, in this case cut "ipHostNumber: " and have remained only IPs.

Thanks in advance, if there's anything missing please let me know.

2
  • what I wanted ... ping it. Then do you need arrays at all? Just ping them. Commented Sep 30, 2021 at 9:54
  • Okay, though I have to cut somehow the string before IP Commented Sep 30, 2021 at 9:55

2 Answers 2

1

Why do you need arrays at all? Just read the input and do the work.

while IFS=' ' read -r _ ip; do
   ping -c1 "$ip"
done < "$destlog"

See https://mywiki.wooledge.org/BashFAQ/001 .

Another way is to use xargs and filtering:

awk '{print $2}' "$destlog" | xargs -P0 -n1 ping -c1
Sign up to request clarification or add additional context in comments.

8 Comments

Well, the first solution works pretty well, thank you, I don't know much from bash. Is there a way to always cut/ignore first line while reading from file?
awk 'NR > 1' ...
Yes, I've seen awk 'NR > 1' and sed 1d, though I see it's not working in this case with while loop or it's wrongly used: sed 1d | while IFS=' ' read -r _ ip do echo "$ip" done < "$destlog"
@blazzeq use < <(tail -n+2 "$destlog") instead of < "$destlog"
|
0

I prefer KamilCuk's xargs solution, but if you just wanted the array because you plan to reuse it -

$: readarray -t ip < <( sed '1d; s/^.* //' file )

Now it's loaded.

$: declare -p ip
declare -a ip=([0]="127.0.0.2" [1]="127.0.0.3")
$: for addr in "${ip[@]}"; do ping -c 1 "$addr"; done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.