How to use if else in a Bash script for IP pinging on Ubuntu 21.04

To Use if else In Bash script for IP pinging on Ubuntu 21.04

Introduction:

Bourne Again Shell (BASH) is a UNIX command-line shell; Ping is primarily used to test internet connections by entering an IP address. IPv4 and IPv6 are supported by Ping.

Step 1: Check the OS version by using the following command

linuxhelp@linuxhelp:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2:Now creating script file named ping.sh

linuxhelp@linuxhelp:~$ vi ping.sh

#!/bin/bash

mail="Mail server is online"
mail1="Mail server is offline"

share="share is online"
share1="share is offline"


if [ "`ping -c 4 192.168.7.235`" ]
then
  echo $mail
else
  echo $mail1
fi


if [ "`ping -c 4 192.168.7.150`" ]
then
  echo $share
else
  echo $share1
fi

Step 3:Now I am running the script file using sh

linuxhelp@linuxhelp:~$ sh ping.sh 
Mail server is online
share is online

By this using of if else in Bash script for IP pinging on Ubuntu 21.04 comes to an end

FAQ
Q
What is Shell Script?
A
Shell Script is a computer program that executes serial of commands.
Q
What is an interpreter on the computer?
A
An interpreter is a computer program that directly executes the program scripts.
Q
What is a string?
A
Strings are any set of characters.
Q
How are variables defined?
A
variables are defined as $ symbol before the name of the variables.
Q
What are variables?
A
variable is any string that is defined by storing any type sring in that.