Operations 6 min read

Validate IP Addresses in Bash: Two Reliable Shell Scripts

This article explains why validating IP addresses is essential in shell scripting, then presents two Bash methods—using awk/grep and regular‑expression matching—followed by a looped version that repeatedly prompts for input until a correct IP is entered, complete with example commands.

ITPUB
ITPUB
ITPUB
Validate IP Addresses in Bash: Two Reliable Shell Scripts

From an operations perspective, checking whether a user‑provided value such as an IP address is syntactically correct is a common task in shell scripts. An IP consists of four decimal numbers separated by dots, each ranging from 0 to 255.

Method 1: awk + grep

#!/bin/bash
# blog: http://lizhenliang.blog.51cto.com
function check_ip() {
    IP=$1
    VALID_CHECK=$(echo $IP | awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
    if echo $IP | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
        if [ ${VALID_CHECK:-no} == "yes" ]; then
            echo "IP $IP available."
        else
            echo "IP $IP not available!"
        fi
    else
        echo "IP format error!"
    fi
}
# Example usage
check_ip 192.168.1.1
check_ip 256.1.1.1

This script first ensures the string matches the dotted‑decimal pattern with grep -E, then uses awk to verify each octet does not exceed 255.

Method 2: Bash regex + cut

#!/bin/bash
# blog: http://lizhenliang.blog.51cto.com
function check_ip() {
    IP=$1
    if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        FIELD1=$(echo $IP | cut -d. -f1)
        FIELD2=$(echo $IP | cut -d. -f2)
        FIELD3=$(echo $IP | cut -d. -f3)
        FIELD4=$(echo $IP | cut -d. -f4)
        if [ $FIELD1 -le 255 -a $FIELD2 -le 255 -a $FIELD3 -le 255 -a $FIELD4 -le 255 ]; then
            echo "IP $IP available."
        else
            echo "IP $IP not available!"
        fi
    else
        echo "IP format error!"
    fi
}
# Example usage
check_ip 192.168.1.1
check_ip 256.1.1.1

This version relies on Bash’s built‑in regular‑expression operator to filter the format, then extracts each octet with cut and checks the numeric range.

Looping Until a Valid IP Is Entered

#!/bin/bash
# blog: http://lizhenliang.blog.51cto.com
function check_ip() {
    local IP=$1
    VALID_CHECK=$(echo $IP | awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
    if echo $IP | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
        if [ "$VALID_CHECK" == "yes" ]; then
            echo "IP $IP available!"
            return 0
        else
            echo "IP $IP not available!"
            return 1
        fi
    else
        echo "IP format error!"
        return 1
    fi
}
while true; do
    read -p "Please enter IP: " IP
    check_ip $IP
    [ $? -eq 0 ] && break
    # loop repeats until a correct IP is provided
    done

The loop repeatedly prompts the user, calls check_ip, and exits only when the function returns success, ensuring the script proceeds with a valid address.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SysadminBashregexshell scriptawkIP validation
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.