MyIP

NOTICE: If you are viewing this on a mobile device, for best viewing results, do rotate your device to landscape mode. Otherwise the script likely will be wrapped to where it will be difficult to read.  Some Screenshots

 
Here is a script I've written to demonstrate how to make a graphical pop-up dialog using Zenity. The  script checks to see if Zenity is installed and pops up an information dialog with the LAN IP for the computer and the IP used to connect to the internet. If Zenity isn't installed then the information is echoed back on the command line. 

Script available (Last updated in Sept. 2019) as a download at the bottom right side of page.  (click the arrow pointing down)

#!/bin/bash
#   _____ ______       ___    ___ ___  ________  
#  |\   _ \  _   \    |\  \  /  /|\  \|\   __  \ 
#  \ \  \\\__\ \  \   \ \  \/  / | \  \ \  \|\  \
#   \ \  \\|__| \  \   \ \    / / \ \  \ \   ____\
#    \ \  \    \ \  \   \/  /  /   \ \  \ \  \___|
#     \ \__\    \ \__\__/  / /      \ \__\ \__\  
#      \|__|     \|__|\___/ /        \|__|\|__|  version 2019.9.9.0
#                   \|___|/                     

# version number is now based on year.month.day.stable

# This version added echo color codes, new version number format,

# and 'Please patient' message and escape codes to delete it.

# MyIP a BASH script by Ongytenes and is licensed under a
# Creative Commons Attribution 4.0 International License.
# You are free to:
#   SHARE — copy and redistribute the material in any medium or format
#   ADAPT — remix, transform, and build upon the material for any purpose, even
#           commercially.
# The licensor cannot revoke these freedoms as long as you follow the license terms.
# More on License: http://creativecommons.org/licenses/by/4.0/

# ABOUT MyIP: https://sites.google.com/site/ongytenes/code/bash/myid
#   MyIP is an educational BASH script. It demonstrates:
#     ▶ How to get IP addresses
#     ▶ How to echo text in color (Added Sept. 9, 2019)
#     ▶ How to echo a line without a carriage return (newline) (Added Sept. 9, 2019)
#     ▶ How to delete current echoed line (Added Sept. 9, 2019)
#     ▶ How to assign variables and the uses of variables
#     ▶ Test if a program is installed
#     ▶ Demonstrates If/Then condition
#     ▶ Demonstrates use of the Zenity Information dialog
#     ▶ Demonstrates the 'echo' command
#     ▶ Demonstrates how to concatenate strings (add strings together)
# ­­­­ Start of executable BASH script ­­­­#  Set Colors for text

NC='\033[0m' # No Color; Used to return to default color
RED='\033[1;31m' # Assign escape code for the light red to variable Red
Yellow='\033[1;33m' # Assign escape code for the yellow color to variable Yellow

# Get Local IP and assign value to $ip1 variable
ip1=$(hostname -I ) #Assigning local IP to variable ip1

# Get External (Internet) IP and assign value to $ip2 variable
echo -ne "${RED}RETRIEVING EXTERNAL IP. Please be patient${NC}" #echo message and do not add new line
ip2=$(wget http://ipinfo.io/ip -qO -) #Assigning external IP to variable ip2
echo -e "\033[2K" #delete 'Please be patient' message line

# Test if the Zenity program is installed
if hash zenity 2>/dev/null; then 
#   If Zenity is installed then popup information dialog with IP info.
      zenity --info --text="<b>External IP:</b> "$ip2"\n<b>Local IP:</b> "$ip1 ­­--title="IP Addresses"
  else
#   If Zenity isn't installed then echo IP info in color on command line.
      echo -e "${RED}Local IP: ${Yellow}"$ip1"\n${RED}External IP: ${Yellow}"$ip2$"${NC}\n"
fi
# ­­­­ End of executable BASH script ­­­­

# * * * References * * *
#
# About BASH
# Bash is the GNU Project's shell. Bash is an acronym for Bourne Again SHell.
# Bash is an sh ­compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh).
#
# Links:
#   Official site:
#      www.gnu.org/software/bash/
#   My own page dedicated to BASH:
#      https://sites.google.com/site/ongytenes/code/bash
#   Wikipedia:
#      https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29
#   Bash Guide for Beginners:
#      http://www.tldp.org/LDP/Bash­Beginners­Guide/html/Bash­Beginners­Guide.html
#   Advanced Bash­Scripting Guide:
#     http://www.tldp.org/LDP/abs/html/
#
# About Zenity
# Zenity is a free software and a cross­platform program that allows the
# execution of GTK+ dialog boxes in command­line and shell scripts.
#
# Links:
#   Zenity Manual
#      https://help.gnome.org/users/zenity/stable/
#   Wikipedia
#      https://en.wikipedia.org/wiki/Zenity