Allan Miller
Allan Miller

Reputation: 1

How to search for and report IP in all files using Linux shell script

I am very new at this:

This Linux script currently works as designed.

I would like to alter or rewrite it to continue to search all 3 files for the IP and report accordingly i.e., "IP is in group 1 and 3" or "IP is in group 1 IP is in group 3"

Looking for some help understanding how to structure it to do so. Thanks.

clear
echo -n "Enter the IP address"
read ip
echo ""
echo "Searching for $ip"
sleep 1
echo ""
if grep -wq $ip /system/config/channels/group1.yml; then
    echo "IP is in group1"
    echo ""
elif grep -wq $ip /system/config/channels/group2.yml; then
    echo "IP is in group2"
    echo ""
elif grep -wq $ip /system/config/channels/group3.yml; then
    echo "IP is in group3"
    echo ""
else
    echo "IP not found. Exiting..."
    echo ""
fi

Upvotes: 0

Views: 74

Answers (1)

Ôrel
Ôrel

Reputation: 7622

As @tink said just remove elif Add a var for the not found message

clear
echo -n "Enter the IP address"
read ip
echo ""
echo "Searching for $ip"
sleep 1
echo ""
not_found=true
if grep -wq $ip /system/config/channels/group1.yml; then
    echo "IP is in group1"
    echo ""
    not_found=false
fi
if grep -wq $ip /system/config/channels/group2.yml; then
    echo "IP is in group2"
    echo ""
    not_found=false
fi
if grep -wq $ip /system/config/channels/group3.yml; then
    echo "IP is in group3"
    echo ""
    not_found=false
fi
if $not_found; then
    echo "IP not found. Exiting..."
    echo ""
fi

Upvotes: 1

Related Questions