Bash Ip If Then Else Statement

Bash IP IF THEN ELSE Statement

use double brackets, spaces are important:

IP=$(ifconfig eth0 | grep inet | grep -v inet6 | cut -c 21-34)
if [[ $IP = 192.168.32.1 ]]; then
mkdir IPFolder-1
elif [[ $IP = 192.168.32.2 ]]; then
mkdir IPFolder-2
fi

Note: on Ubuntu 11.10, I had to use cut -c 21-34, adapt as necessary.

IP address in condition statement bash profile

@GordonDavisson suggested:

if ip add show tun0 2>/dev/null
then
export http_proxy='http://127.0.0.1:2123'
fi

but you could also write it like this:

ip add show tun0 2>/dev/null && export http_proxy='http://127.0.0.1:2123'

ip will print stuff on stdout when successful, so maybe you want to do >& /dev/null? Instead of throwing all the data away, consider storing it in a variable, then you can always add a verbose flag to your program at some later point to print the content of that variable if you need to debug it.

Check for IP validity

If you're using bash, you can do a simple regex match for the pattern, without validating the quads:

#!/usr/bin/env bash

ip=1.2.3.4

if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "success"
else
echo "fail"
fi

If you're stuck with a POSIX shell, then you can use expr to do basically the same thing, using BRE instead of ERE:

#!/bin/sh

ip=1.2.3.4

if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
echo "success"
else
echo "fail"
fi

Note that expr assumes that your regex is anchored to the left-hand-side of the string, so the initial ^ is unnecessary.

If it's important to verify that each quad is less than 256, you'll obviously require more code:

#!/bin/sh

ip=${1:-1.2.3.4}

if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
for i in 1 2 3 4; do
if [ $(echo "$ip" | cut -d. -f$i) -gt 255 ]; then
echo "fail ($ip)"
exit 1
fi
done
echo "success ($ip)"
exit 0
else
echo "fail ($ip)"
exit 1
fi

Or perhaps even with fewer pipes:

#!/bin/sh

ip=${1:-1.2.3.4}

if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
IFS=.
set $ip
for quad in 1 2 3 4; do
if eval [ \$$quad -gt 255 ]; then
echo "fail ($ip)"
exit 1
fi
done
echo "success ($ip)"
exit 0
else
echo "fail ($ip)"
exit 1
fi

Or again, if your shell is bash, you could use a cumbersome regular expression for quad validation if you're not fond of arithmetic:

#!/usr/bin/env bash

ip=${1:-1.2.3.4}

re='^(0*(1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))\.){3}'
re+='0*(1?[0-9]{1,2}|2([‌​0-4][0-9]|5[0-5]))$'

if [[ $ip =~ $re ]]; then
echo "success"
else
echo "fail"
fi

This could also be expressed in BRE, but that's more typing than I have in my fingers.

And lastly, if you like the idea of putting this functionality ... in a function:

#!/usr/bin/env bash

ip=${1:-1.2.3.4}

ipvalid() {
# Set up local variables
local ip=${1:-NO_IP_PROVIDED}
local IFS=.; local -a a=($ip)
# Start with a regex format test
[[ $ip =~ ^[0-9]+(\.[0-9]+){3}$ ]] || return 1
# Test values of quads
local quad
for quad in {0..3}; do
[[ "${a[$quad]}" -gt 255 ]] && return 1
done
return 0
}

if ipvalid "$ip"; then
echo "success ($ip)"
exit 0
else
echo "fail ($ip)"
exit 1
fi

There are many ways you could do this. I've shown you just a few.

Bash 'if' statement in 'for' loop in an 'if' statement

The if statement needs to be inside the loop.

for i in $(seq $first $last)
do
ping -c 1 ${ip[0]}.${ip[1]}.${ip[2]}.$i > /dev/null
if [ $? -eq 0 ]; then
echo "${ip[0]}.${ip[1]}.${ip[2]}.$i is up"
else
echo "${ip[0]}.${ip[1]}.${ip[2]}.$i is down"
fi
done

Checking host availability by using ping in bash scripts

Ping returns different exit codes depending on the type of error.

ping 256.256.256.256 ; echo $?
# 68

ping -c 1 127.0.0.1 ; echo $?
# 0

ping -c 1 192.168.1.5 ; echo $?
# 2

0 means host reachable

2 means unreachable

Ping Test If-else case

If you are using the standard GNU/Linux ping tool, then the manual states:

If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.

This means you can capture the exit code from the command in the shell and switch on that. For bash:

if ping -c1 192.168.1."$i" ; then
echo "Unit ${i} is online"
else
echo "Unit ${i} is offline"
fi

Or as a one-liner using || and &&:

ping -c 192.168.1."$i" && echo "Unit ${i} is online" || echo "Unit ${i} is offline"


Related Topics



Leave a reply



Submit