Red hat linux Shell scripting

Color text output on bash scripts

Users who have been using Linux for awhile often learn that creating a basic script is a good way to run multiple, often-repeated commands. Adding a little color to scripts can additionally provide nice feedback. This can be done in a fairly straight-forward way by using the tput command.

A common way of doing this is to define the colors that tnput can produce by putting them at the beginning of the bash script:

#!/bin/bash
# tputcolors

red="\033[00;31m"
RED="\033[01;31m"

green="\033[00;32m"
GREEN="\033[01;32m"

brown="\033[00;33m"
YELLOW="\033[01;33m"

blue="\033[00;34m"
BLUE="\033[01;34m"

purple="\033[00;35m"
PURPLE="\033[01;35m"

cyan="\033[00;36m"
CYAN="\033[01;36m"

white="\033[00;37m"
WHITE="\033[01;37m"

NC="\033[00m"

echo -e "${GREEN}**********************************************************************************"
echo -e "${GREEN}**                                                                              **"
echo -e "${GREEN}**      _____    _                   _____       _       _   _                  **"
echo -e "${GREEN}**     |_   _|  | |                 / ____|     | |     | | (_)                 **"
echo -e "${GREEN}**       | |  __| | ___ _   _ ___  | (___   ___ | |_   _| |_ _  ___  _ __  ___  **"
echo -e "${GREEN}**       | | / _  |/ _ \ | | / __|  \___ \ / _ \| | | | | __| |/ _ \| '_ \/ __| **"
echo -e "${GREEN}**      _| || (_| |  __/ |_| \__ \  ____) | (_) | | |_| | |_| | (_) | | | \__ \ **"
echo -e "${GREEN}**     |_____\__,_|\___|\__,_|___/ |_____/ \___/|_|\__,_|\__|_|\___/|_| |_|___/ **"
echo -e "${GREEN}**                                                                              **"
echo -e "${GREEN}**                        Powered By Ideus Solutions                            **"
echo -e "${GREEN}**                                                                              **"
echo -e "${GREEN}**        Web:www.ideus.lk   Email:prabath@ideus.lk   Mobile:(+94)71 5770488    **"
echo -e "${GREEN}**                                                                              **"
echo -e "${GREEN}**                                                                              **"
echo -e "${GREEN}**********************************************************************************"
echo -e ""
echo -e "${RED}Unauthorized access to this machine is prohibited"
echo -e "${RED}Only authorized System Administrator can access to this system Press  if you are not an authorized user"

CPUMOD=$(cat /proc/cpuinfo | grep -m 1 -w 'model name' | awk -F: '{print $2}')
HOSTNAME=$(uname -n)
KERNEL=$(uname -r)
#MEMTOTAL=$(cat /proc/meminfo | grep -m 1 -w 'MemTotal' | awk -F: '{print $2}')
#MEMFREE=$(cat /proc/meminfo | grep -m 1 -w 'MemFree' | awk -F: '{print $2}')
#SWAPTOTAL=$(cat /proc/meminfo | grep -m 1 -w 'SwapTotal' | awk -F: '{print $2}')
#SWAPFREE=$(cat /proc/meminfo | grep -m 1 -w 'SwapFree' | awk -F: '{print $2}')

MEMTOTAL=$(free -m | sed -n -e '2{p;q}' | awk -F ' ' '{print $2}')
MEMFREE=$(free -m | sed -n -e '2{p;q}' | awk -F ' ' '{print $3}')
SWAPTOTAL=$(free -m | sed -n -e '4{p;q}' | awk -F ' ' '{print $2}')
SWAPFREE=$(free -m | sed -n -e '4{p;q}' | awk -F ' ' '{print $2}')


df1=$(df -h| sed -n -e '1{p;q}')
df2=$(df -h| sed -n -e '2{p;q}')
df3=$(df -h| sed -n -e '3{p;q}')

echo -e ""
echo -e "${WHITE} Welcome ${YELLOW}${USER} ${WHITE} to the Ideus Solutions Web Server"
echo -e ""

echo -e "${blue} Date: "`date`
echo -e ""

echo -e "${WHITE} Hostname:   ${HOSTNAME}"
echo -e "${WHITE} CPU Model: ${CPUMOD}"
echo -e ""
echo -e "${WHITE} Total Memory: ${MEMTOTAL}MB"
echo -e "${WHITE} Free Memory: ${MEMFREE}MB"
echo -e ""
echo -e "${WHITE} Swap Total:    ${SWAPTOTAL}MB"
echo -e "${WHITE} Swap Free:    ${SWAPFREE}MB"
echo -e ""
echo -e " Disk Usage :"
echo -e " ${YELLOW}${df1}"
echo -e " ${YELLOW}${df2}"
echo -e " ${YELLOW}${df3}"

# Reset Terminal Colour Back to Normal
echo -e "${NC}"

Leave a Reply

Your email address will not be published. Required fields are marked *