So I have my targets array which is a bunch of IP read from a file, now I am trying to cat a file that contains my IP and results from ssh command, ut because I am trying 3 different passwords on every target I will get 2 denials and one job scheduled result. So I figured, I will check if a job was scheduled for a particular result and I will print "$ip OK, if not then I will print "$ip failed" here is what I have :
#!/bin/bash
for ip in "${targets_array[@]}"
do
cat "$output_file" | awk -v ip="$ip" '/$ip/&&/job/ {result="OK"} END {
!result?result="failed":result=result;}'
done
Pseudocode of what I am trying to do: 1.cat from output file line of the file where you can find desired ip and the word "job" 2.1. if you found it print append to a string variable "$ip OK" 2.2 if not found append to a string variable "$ip failed" 3. override output file with the result stored in the variable
but when I run it in debugging mode I see that the value was never passed to the awk script. How can I pass it?
+ awk -v ip= '/$ip/&&/job/ {result="OK"} END {
!result?result="failed":result=result;}'
the file I am cutting from:
unwantedString 124.131.8010 Permission denied, please try again.
unwantedString 125.124.90.134 Permission denied, please try again.
unwantedString 145.120.100.8 Permission denied, please try again.
unwantedString 145.101.100.158 Permission denied, please try again.
unwantedString 124.131.80.2 Permission denied, please try again.
unwantedString 125.124.90.134 job 32 at 2020-12-16 23:30
unwantedString 145.120.100.8 Permission denied, please try again.
unwantedString 145.101.100.158 job 27 at 2020-12-16 23:30
unwantedString 124.131.8010 Permission denied, please try again.
unwantedString 125.124.90.134 Permission denied, please try again.
unwantedString 145.120.100.8 Permission denied, please try again.
unwantedString 145.101.100.158 Permission denied, please try again.
set -x
outputs withfor ip in a b c; do cat /dev/null | awk -v ip="$ip" 'BEGIN{print ip}'; done
versusfor ip in a b c; do awk -v ip="$ip" 'BEGIN{print ip}'; done
– steeldriver Dec 16 '20 at 19:22