Interesting way to use colors (an easy way) in bash. And a cool way to use shift.

This is from a script that I found online that install GNS3 and vpcs and the like. So this is just a bash code study on it.

COLORS

First set the colors

## Counters and constants
stageCount=1
black=0
red=1
green=2
yellow=3
blue=4
purple=5
cyan=6
white=7
inverseOn=$(tput rev)
boldOn=$(tput bold)
normal=$(tput sgr0)
ulOn=$(tput smul)
ulOff=$(tput rmul)
whiteOnBlueOn=$(tput setab $blue;tput setaf $white)
brightWhiteOnBlueOn=$(tput setab $blue;tput setaf $white;tput bold)
brightWhiteOnRedOn=$(tput setab $red;tput setaf $white;tput bold)
brightWhiteOnGreenOn=$(tput setab $green;tput setaf $white;tput bold)

Example of use:

# ---- with echo ----"

echo -e $brightWhiteOnBlueOn"############### Chris' Crude GNS3 WB installer : version $installerVersion ###############"$normal

# ---- with read ----"

read -p "Press$boldOn <Enter>$normal" enterKey

 SHIFT

First make the function that uses shift.

This will install apps via the function.  In the function is a loop ( the loop as big as the number of arguments fed into installApps function)

function installApp ()
#$1 must be true or false to indicate "Interactive y/n"
#$2 Friendly app name eg SSH
#$3... list of apps that have to be got with apt-get install
{
interactive=$1
appName=$2
count=$#
if $interactive ; then
	read -p "Install $appName? y/n[y]" response
else
	response="y"
fi
returnValue=0

if [ "$response" = "y" ] || [ "$response" = "" ] ; then
	for (( i=3; i<=$count; i++ )) do
		doInstall=true
		while  $doInstall ; do
			sudo apt-get install $3 -y
			if [ "$?" = "0" ] ; then
				doInstall=false
			else
				echo -e "\n\nThere was a problem installing $3"
				read -p "Try again y/n [y] " installResponse
				if [[ "$installResponse" =~ ^[n|N] ]] ; then
					doInstall=false
					returnValue=1
		 		fi
			fi
		done
		shift
	done
else
	echo "$appName not installed"
fi
return $returnValue	
}

Usage of installapp function:

installApp $interactive "NIO Tap adapter" uml-utilities bridge-utils
installApp $interactive "SSH Server" openssh-server 
installApp $interactive "htop utility" htop 
installApp $interactive CPULimit cpulimit 
installApp $interactive VirtualBox VirtualBox xdotool
installApp $interactive XTerm xterm
installApp $interactive PuTTY putty
installApp $interactive ROXTerm roxterm
installApp $interactive Konsole konsole
installApp $interactive Dynamips dynamips
installApp $interactive GNS3 gns3
installApp $interactive "Virtual PC Simulator" vpcs

 Another interesting bit

if $interactive ; then
	read -p "Update repositories? y/n[y]" response
fi
if [  "$response" = "y" ] || [	 "$response" = ""  ] ; then
	if [ -f /var/lib/apt/lists/lock ] ; then sudo rm /var/lib/apt/lists/lock ; echo "/var/lib/apt/lists/lock removed" ; fi
	if [ -f /var/cache/apt/archives/lock ] ; then sudo rm /var/cache/apt/archives/lock ; echo "/var/cache/apt/archives/lock removed" ;fi
	echo  "$brightWhiteOnRedOn Press <Enter> when requested$normal"
        sudo add-apt-repository ppa:gns3/ppa
	sudo apt-get update -y
else
	echo "Repositories not updated"
fi

 

Leave a Reply

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