Welcome to Techy Tunes !

Follow Me

Execute Custom Shell Script as a Command Using Aliases



By   Unknown      October 02, 2016      Labels: , , , , , , , , ,  
As a developer you might want to work with different development environments. As an example, I had to work with Java 1.4, 1.5, 1.7 and 1.8 :D . When JDK changes all other stuff such as maven will also have to change.

One thing you can do is have everything in you bashrc or bash_profile and comment and uncomment the required environment variables. Believe me, I did that for a long time. duh...

Or else, you can have scripts to set your environment !!! (yay).  One sh file would look like this

#!/bin/sh
export JAVA_HOME=/home/isuru/installs/jdk/jdk1.8.0_101
export PATH=$JAVA_HOME/bin:$PATH

and the other would look like this

#!/bin/sh
export JAVA_HOME=/home/isuru/installs/jdk/jdk1.7.0_79
export PATH=$JAVA_HOME/bin:$PATH

But we can't memorize where this files are to locate them always. That's where alias comes in.

Create .bash_aliases file in your home directory. Check whether this file is being sourced inside your bashrc or bash_profile.

Add aliases to the ~/.bash_aliases as follows
alias java8='. /home/isuru/stuff/scripts/java8.sh'
alias java7='. /home/isuru/stuff/scripts/java7.sh'
In my machine both the script files are located under the directory /home/isuru/stuff/scripts. Make sure the sh files have execute permissions.

Now we are done here. Open a new shell and type in java8. and then java  -version. Type in java7 and the java -version. See the magic :P

If you are following a routine everyday executing shell scripts, or some other stuff, put them in a single sh file and do register it as an alias. Will make your life easier.

If opening a new terminal really hurts, you can source the ~/.bashrc or ~/.bash_profile file. You can stay where you are in the current terminal and yet get the benefits of alias. See below to uncover a secret ;)

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

When we open a new terminal the ~/.bashrc or ~/.bash_profile automatically sources :D :D

About Unknown

A Software Engineer who desperately loves Music. Works in WSO2 Telco . Loves cricket, hates Buriyani.

No comments :

Post a Comment