'How to test value in files and output read another variable which tally to test value

I need advice as per the below situation

cat file1

ServerA|2

ServerB|1

so I need to know how can I test value no 2 which is not meet 2 = 2 then print value no 1

**in the above we know that ServerB is not met.

Sample output

ServerB has a value 1

** But let's says all value met 2=2; then the output will

All are Good

Thank you in advanced



Solution 1:[1]

You could use this:

file1

ServerA|2
ServerB|1

Script

#!/bin/bash

allfine=true

while IFS= read -r line
do
    read -r firstfield secondfield <<< "$(echo "$line" | awk -F'|' '{print $1 " " $2}')"
    if [[ $secondfield -eq 1 ]]
    then
        echo "$firstfield has a value 1"
        allfine=false
    fi
done < file1

if [[ "$allfine" == "true" ]]
then
    echo "All are Good"
fi
  • read the file line per line
  • extract the first and second value, using | as separator
  • if the second value is == 1, print a message
  • if all values == 2, print a message based on the value of variable allfine

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nic3500