Monday, March 28, 2016

LINUX Bash Script skeleton code

I have been writing very simple Bash scripts for several years.  They are not complex, but, they help me automate tasks at home and work.  If I have to do something more than once I prefer to have a script automate that task. Over the years I have found that there are a lot of excellent on-line sites with information that helped me do my scripts.

From time-to-time this blog will be updated with new/updated skeleton script.

One site that I like is for color in Bash echo commands and they helped me when I was starting my use of color in scripts.  Most of my scripts don't need color, but, now and then I like to highlight something.  UBUNTU has a great forum area and I found a very nice script that allows me to quickly and easily change my background image. The link is here.

One of the first tasks I do with a new machine using LINUX and Z/OS Unix is to create a script library and place my skeleton bash template in that library.  I won't post the one from work as that is specific to the Z/OS environment and the client I work for.  The one I am posting is my personal template. When I find that I am using a piece of bash code on a regular basis I put it into the template and make it easier for me to find that one piece in the future.

My skeleton bash script is below.  I try to work with a basic naming standard.  User defined variables start with "u" and subroutines start with "z".  Everyone has their preferences, but, this is one I have been using for years and I am comfortable with it.  Version is showing as one as I finally got around to putting comments in and I figured that 1.00.00 is a good place to start.

START OF BASH SCRIPT


#! /bin/bash

# This is a skeleton script that has some of my bash codes commonly used
# in my scripts.
#
# Version:  1.00.00 - 2016/03/28 - TPT.  Initial version.
#

# ANSI Colors
zSetANSI() {

# Foreground colors:
    uFGBK="\033[30m";   #Black
    uFGLRD="\033[31m";  #Low  Red
    uFGHRD="\033[91m";  #High Red
    uFGLGR="\033[32m";  #Low  Green
    uFGHGR="\033[92m";  #High Green
    uFGLYW="\033[33m";  #Low  Yellow
    uFGHYW="\033[93m";  #High Yellow
    uFGLBU="\033[34m";  #Low  Blue
    uFGHBU="\033[94m";  #High Blue
    uFGLMG="\033[35m";  #Low  Magenta
    uFGHMG="\033[95m";  #High Magenta
    uFGLCY="\033[36m";  #Low  Cyan
    uFGHCY="\033[96m";  #High Cyan
    uFGHGY="\033[37m";  #High Gray
    uFGLGY="\033[90m";  #Low  Gray
    uFGWT="\033[97m";   #White

# Background colors:   
    uBGBK="\033[40m";   #Black
    uBGLRD="\033[41m";  #Low  Red
    uBGHRD="\033[101m"; #High Red
    uBGLGR="\033[42m";  #Low  Green
    uBGHGR="\033[102m"; #High Green
    uBGLYW="\033[43m";  #Low  Yellow
    uBGHYW="\033[103m"; #High Yellow
    uBGLBU="\033[44m";  #Low  Blue
    uBGHBU="\033[104m"; #High Blue
    uBGLMG="\033[45m";  #Low  Magenta
    uBGHMG="\033[105m"; #High Magenta
    uBGLCY="\033[46m";  #Low  Cyan
    uBGHCY="\033[106m"; #High Cyan
    uBGLGY="\033[47m";  #Low  Gray
    uBGHGY="\033[107m"; #High Gray

# Underline:
    uFGULINE="\033[4m";
   
# Blink:
    uFGBLINK="\033[5m";
   
# Reset all:   
    uFGRESET="\033[0m"
}


#
#  Get the system clock and set to a variable.
#

zGetDATE() {

# System date.  For all date options type date --h on command line.

    uSYSYY=$(date +%Y);
    uSYSMM=$(date +%m);
    uSYSDY=$(date +%d);
    uSYSDT=$uSYSYY$uSYSMM$uSYSDY;
   
    uSYSHH=$(date +%H);
    uSYSMIN=$(date +%M);
    uSYSSEC=$(date +%S);
    uSYSTM=$uSYSHH$uSYSMIN$uSYSSEC;
   
    uSYSJDT=$(date +%j);
    uSYSWK=$(date +%W);
    uSYSDOW=$(date +%w);
    uSYSDAY=$(date +%A)   
    uSYSMTH=$(date +%B)
}

# ============================================================
# END OF ALL SUBROUTINES FOR THIS SCRIPT.
# ============================================================

zSetANSI;
zGetDATE;

# Define the working variable for return codes:
uRC=0;

# Get current directory name
uDir=$( cd "$( dirname "$0" )" && pwd);

# Get process ID
uProcID=$$


#  Now echo out the stuff to the console:

clear;
echo "${uFGHRD}Process Information${uFGRESET}";
echo ;
echo "  ${uFGLGR}Directory........: " $uFGHGR $uDir;
echo "  ${uFGLGR}Process id.......: " $uFGHGR $uProcID;
echo "  ${uFGLGR}Directory........: " $uFGHGR $LOGNAME;
echo ;
echo $uFGHRD;
echo "Date information:" $uFGRESET;
echo "  ${uFGLYW}uSYSDT...........: " $uFGHYW $uSYSDT;
echo "  ${uFGLYW}uSYSTM...........: " $uFGHYW $uSYSTM;
echo "  ${uFGLYW}uSYSJDT..........: " $uFGHYW $uSYSJDT;
echo "  ${uFGLYW}uSYSWK...........: " $uFGHYW $uSYSWK;
echo "  ${uFGLYW}uSYSDOW..........: " $uFGHYW $uSYSDOW;
echo "  ${uFGLYW}uSYSDAY..........: " $uFGHYW $uSYSDAY;
echo "  ${uFGLYW}uSYSMTH..........: " $uFGHYW $uSYSMTH;

echo $uFGRESET;
echo $uFGHYW;
echo "ANSI Foreground color codes:" $uFGRESET;
echo "  RED..............: ${uFGLRD}Low ${uFGHRD}HIGH" $uFGRESET;
echo "  GREEN............: ${uFGLGR}Low ${uFGHGR}HIGH" $uFGRESET;
echo "  YELLOW...........: ${uFGLYW}Low ${uFGHYW}HIGH" $uFGRESET;
echo "  BLUE.............: ${uFGLBU}Low ${uFGHBU}HIGH" $uFGRESET;
echo "  MAGENTA..........: ${uFGLMG}Low ${uFGHMG}HIGH" $uFGRESET;
echo "  CYAN.............: ${uFGLCY}Low ${uFGHCY}HIGH" $uFGRESET;
echo "  GRAY.............: ${uFGLGY}Low ${uFGHGY}HIGH" $uFGRESET;

echo ;
echo $uFGHYW;
echo "ANSI Background color codes:" $uFGRESET;
echo "  RED..............: ${uBGLRD}Low ${uBGHRD}HIGH" $uFGRESET;
echo "  GREEN............: ${uBGLGR}${uFGBK}Low ${uBGHGR}HIGH" $uFGRESET;
echo "  YELLOW...........: ${uBGLYW}${uFGBK}Low ${uBGHYW}HIGH" $uFGRESET;
echo "  BLUE.............: ${uBGLBU}Low ${uBGHBU}${uFGBK}HIGH" $uFGRESET;
echo "  MAGENTA..........: ${uBGLMG}Low ${uBGHMG}${uFGBK}HIGH" $uFGRESET;
echo "  CYAN.............: ${uBGLCY}Low ${uBGHCY}${uFGBK}HIGH" $uFGRESET;
echo "  GRAY.............: ${uBGLGY}${uFGBK}Low ${uBGHGY}HIGH" $uFGRESET;


echo ;
echo $uFGHYW;
echo "ANSI Other color codes:" $uFGRESET;
echo ;
echo "  UNDERLINE........: ${uFGULINE}Underline" $uFGRESET;
echo "  BLINK............: ${uFGBLINK}Blink" $uFGRESET;
echo ;
echo "Script finishing with return code: " $uRC;


exit $uRC;

# ----------  END OF SCRIPT ----------


No comments: