• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

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

  • 00:35 lsb_release -a
  • 00:52 vi ping.sh
  • 07:00 sh ping.sh
6744

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

Tags:
keeljohnston
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

What are variables?

A

variable is any string that is defined by storing any type sring in that.

Q

How are variables defined?

A

variables are defined as $ symbol before the name of the variables.

Q

What is a string?

A

Strings are any set of characters.

Q

What is an interpreter on the computer?

A

An interpreter is a computer program that directly executes the program scripts.

Q

What is Shell Script?

A

Shell Script is a computer program that executes serial of commands.

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

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

How to install Meld tool in Ubuntu
How to install Meld tool in Ubuntu
Feb 25, 2017
How to install Dconf-Editor on Ubuntu 18.04
How to install Dconf-Editor on Ubuntu 18.04
Jul 14, 2018
How to install and update OpenSSL on Ubuntu 16.04
How to install and update OpenSSL on Ubuntu 16.04
Mar 9, 2017
How to install GLib 2.0 on Ubuntu 17.04
How to install GLib 2.0 on Ubuntu 17.04
May 22, 2017
How to Install Android Emulator on Ubuntu 20.4.1
How to Install Android Emulator on Ubuntu 20.4.1
Jul 13, 2021
How To Install AnyDesk on Ubuntu 16.04
How To Install AnyDesk on Ubuntu 16.04
Apr 4, 2018
How to install Genymotion 2.12.1 on Ubuntu 18.04
How to install Genymotion 2.12.1 on Ubuntu 18.04
Jul 9, 2018
How to install Timeshift 18.4 on Ubuntu 18.04
How to install Timeshift 18.4 on Ubuntu 18.04
Jul 6, 2018

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

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

Ubuntu
matthew class=
Failed to enable unit: Refusing to operate on linked unit file sshd.service
Apr 15, 2019
Ubuntu
mason class=
Passwd: You may not view or modify password information for root On Ubuntu 19.04
May 27, 2019
Ubuntu
isaac class=
/etc/apt/sources.list Permission denied
May 18, 2017
Ubuntu
yousuf class=
lsb_release command not working : Debian
Jan 18, 2018
ifconfig command
jackbrookes class=
what is the location of the ifconfig program on your machine?
Jan 4, 2018
Ubuntu
mason class=
"E: Package 'php-mcrypt' has no installation candidate" error on Ubuntu 20.4.1
Mar 15, 2021
NFS
luke class=
clnt_create: RPC: Program not registered
Apr 25, 2017
Apache
isaac class=
How to disable apache welcome page on Ubuntu
Dec 15, 2018

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

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

How To Install Mixxx on Ubuntu 16.04
How To Install Mixxx on Ubuntu 16.04
Oct 11, 2017
Ubuntu 17.04 released with greater expectations
Ubuntu 17.04 released with greater expectations
Apr 15, 2017
Ubuntu Core now available on i.MX6 based TS-4900 thanks to Technologic Systems Inc.
Ubuntu Core now available on i.MX6 based TS-4900 thanks to Technologic Systems Inc.
Mar 1, 2017
Ubuntu 17.10 Artful Aardvark Beta 1 is now here. Download Now
Ubuntu 17.10 Artful Aardvark Beta 1 is now here. Download Now
Sep 2, 2017
Ubuntu Unity is no more: One Linux dream has been axed
Ubuntu Unity is no more: One Linux dream has been axed
Apr 7, 2017
What’s next for Ubuntu Linux Desktop?
What’s next for Ubuntu Linux Desktop?
Apr 11, 2017
Say Hi to Ubuntu's new mascot
Say Hi to Ubuntu's new mascot
Mar 22, 2019
KDE Connect App was removed from Google Play Store and brought back in 24 hours
KDE Connect App was removed from Google Play Store and brought back in 24 hours
Mar 22, 2019
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Sebastian ?
How to change non required to required field in SuiteCRM Custom/Default Modules

How to change not required to the required field in SuiteCRM Custom/Default Modules?

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.