Ask for confirmation before running bash command

Sometimes we need to confirm or give warning before running any command.

In my case, I need to check JAVA version before building android source.
Android L needs to be built with Java 1.7 and Android KK needs to be built with Java 1.6. So it is required to change JAVA version while switching between Android K and L.

If we forget to change JAVA version, it tries to build source with improper version consuming lots of time (as java version changes, it tries to rebuild all the things) and results in failure at some point.

To avoid this, I updated bash script so that before build command it checks for JAVA version and asks user if it is proper or not. Upon confirmation from user it proceeds further.

Below is script I wrote and included in bashrc using “source” command at end of bashrc.


#!/bin/bash

function jdkmsg {
echo -e "\e[38;5;82m"
echo "########################################################################"
echo -e "\tHope You are using correct JDK version\n"
echo -e "JAVA_HOME = $JAVA_HOME"
java -version
javac -version
echo -e "You can change java and javac versions using below commands:"
echo -e "sudo update-alternatives --config java and"
echo -e "sudo update-alternatives --config javac"
echo "########################################################################"
echo -e "\e[39;5;82m"
}

debug_trap () {
	if [[ "$BASH_COMMAND" == *"build.sh"* ]]
	then
		jdkmsg
		echo "Allow?"
		select choice in yes no
		do
			if [ "$choice" = "yes" ]
				then break
			elif [ "$choice" = "no" ]
			then return 1
			fi
		done
	fi
}

shopt -s extdebug
trap debug_trap DEBUG

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s