You are reading variable wrong with awk
, see here:
How to use shell variables in an awk script
#!/bin/bash
svr=(abc.net def.net)
for n in ${svr[*]}; do
awk -vSVR="$n" '$1=="string" {f=1} END {if (f) print $1, SVR,"OK";}' input.txt
done
Using array is a better way to store server name.
If you like to test the file and only get on output is ok, its better to print result in an END
block.
PS this still does not work correctly, since its not clear what you like to test where.
This is an attempt to see if I am on correct track.
You have a set of files. You like to test to see if the files have a specific settings.
cat abc.net
string home
More data
cat def.net
string work
more data
cat test
#!/bin/bash
svr=(abc.net def.net)
for n in ${svr[*]}; do
awk -vI=$(cat input) '/string/ {if ($2~I) f=1} END {if (f) print FILENAME,"OK"}' "$n"
done
With input
file set to test for work
cat input
work
./test
def.net OK
Then setting input
to home
cat input
home
./test
abc.net OK
If there is only one hit in every file, you could shorten it some and remove the loop
cat test
#!/bin/bash
svr=(abc.net def.net)
awk -vI=$(cat input) '/string/ {if ($2~I) print FILENAME,"OK"}' ${svr[*]}