Snippets

Peter Scargill Include for Jessie Script - call it hackitt_includes.sh

Created by Peter Scargill
# Get time as a UNIX timestamp (seconds elapsed since Jan 1, 1970 0:00 UTC)
startTime="$(date +%s)"
columns=$(tput cols)
user_response=""

# Reset
Color_Off='\e[0m'       # Text Reset

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)

# High Intensity
IBlack='\e[0;90m'       # Black
IRed='\e[0;91m'         # Red
IGreen='\e[0;92m'       # Green
IYellow='\e[0;93m'      # Yellow
IBlue='\e[0;94m'        # Blue
IPurple='\e[0;95m'      # Purple
ICyan='\e[0;96m'        # Cyan
IWhite='\e[0;97m'       # White

# Bold High Intensity
BIBlack='\e[1;90m'      # Black
BIRed='\e[1;91m'        # Red
BIGreen='\e[1;92m'      # Green
BIYellow='\e[1;93m'     # Yellow
BIBlue='\e[1;94m'       # Blue
BIPurple='\e[1;95m'     # Purple
BICyan='\e[1;96m'       # Cyan
BIWhite='\e[1;97m'      # White
#!/bin/bash

skip=0

clean_stdin()
{
while read -r -t 0; do
    read -n 256 -r -s
done
}

# Permanent loop until both passwords are the same..
function user_input {
  local VARIABLE_NAME=${1}
  local VARIABLE_NAME_1="A"
  local VARIABLE_NAME_2="B"
  while true; do
	  printf "${BICyan}$2: ${BIWhite}";
	  if [ "$3" = "hide" ] ; then
		stty -echo;
	  fi
	  read VARIABLE_NAME_1; 
	  stty echo;
	  if [ "$3" = "hide" ] ; then
	  	  printf "\n${BICyan}$2 (again) : ${BIWhite}";
		  stty -echo;
		  read VARIABLE_NAME_2;
		  stty echo;
	  else 
	      VARIABLE_NAME_2=$VARIABLE_NAME_1;
	  fi 
	  if [ $VARIABLE_NAME_1 != $VARIABLE_NAME_2 ] ; then
		 printf "\n${BIRed}Sorry, did not match!${BIWhite}\n";
	  else
		 break;
	   fi 
  done
  readonly ${VARIABLE_NAME}=$VARIABLE_NAME_1;
  if [ "$3" == "hide" ] ; then
     printf "\n";
  fi	 
}

timecount(){
    sec=30
    while [ $sec -ge 0 ]; do
        printf "${BIPurple}Continue Y(es)/n(0)/s(kip)/a(ll)-  00:0$min:$sec remaining\033[0K\r${BIWhite}"
        sec=$((sec-1))
		trap '' 2
		stty -echo
		read -t 1 -n 1 user_response
		stty echo
		trap - 2
		if [ -n  "$user_response" ]; then
			break
		fi					
    done
}

task_start(){
printf "${BIGreen}%*s\n" $columns | tr ' ' -
printf "$1"
clean_stdin
skip=0
printf "\n%*s${BIWhite}\n" $columns | tr ' ' -
elapsedTime="$(($(date +%s)-startTime))"
printf "Elapsed Time: %02d hrs %02d mins %02d secs\n" "$((elapsedTime/3600%24))" "$((elapsedTime/60%60))" "$((elapsedTime%60))"
clean_stdin
if [ "$user_response" != "a" ]; then
	timecount
fi
echo -e "                                                                        \033[0K\r"
if  [ "$user_response" = "n" ]; then
	printf "${BIWhite}"
	exit 1
fi	
if  [ "$user_response" = "s" ];then
	skip=1
fi	
if [ -n  "$2" ]; then
	if [ $skip -eq 0 ]; then
		printf "${BIYellow}$2${BIWhite}\n"
	else
		printf "${BICyan}%*s${BIWhite}\n" $columns '[SKIPPED]'
	fi
fi
}

task_end(){
	printf "${BICyan}%*s${BIWhite}\n" $columns '[OK]'
}

Comments (0)