* * *

Ever thought of boosting you commands and making them even more powerful and informative? Well Linux provides you the power to mould commands as per your wills and thus make them even more powerful.
This will be a very short and compact article wherein we try to achieve 2 things.
1) As soon as I open up my terminal, it welcomes me ans shows me the current RAM utilization.

Greeting tex when the terminal is opened

2) Customized cd command to prompt when you enter any folder which is a git repository 

Repository details in a folder which is a repository.

 

Let's get into action.

Welcome message as soon as you open the Konsole

Every time you open BASH  terminal the content of .bashrc file are executed. That is the reason almost all your environment variables, prompt configurations( described in the previous post).
We can declare bash functions and call them right away to get the wanted effect. What makes it all tricky is getting the only desirable outputs from the commands and manipulating them. Let's analyze our greeting message.

Welcome to the war zone shubh.
RAM consumption is 44%.

 

Careful inspection tells us that the only dynamic content here is the username (Shubh) and the RAM consumption figure (44). Rest all is a static string which isn't a matter of concern.

We can use a predifned command uname -a to get the username . The difficulty here is, along with the username it gives a lot of other informations which we don't need for this case. We use one of linux wonders awk to select only the required part of the output of the command.

uname -a | awk '{print $2}'

 

It only prints the second word from output of uname -a which happens to be the username. Next we wrap it into proper string format and enclose it in a function . The next part (calculating percent usage of RAM) is a bit more complicated. We need to first get the total RAM size , the RAM used  and then convert it into percentage utilization. free -m is a command again which comes to our rescue but it too has to be paired with awk to get proper results. We use two such instances one to get free memory and the other to get the total ram size.

free -m | awk 'FNR == 2 {print $2}' //to get total RAM size
free -m | awk 'FNR == 2 {print $3}' // to get total RAM used

 

And that's it, wrap it all in a function and you are good to go.

freemem() {
    total="$(free -m | awk 'FNR == 2 {print $2}')"
    freem="$(free -m | awk 'FNR == 2 {print $3}')"
    z=$((freem*100 / total))
    echo -n "RAM consumtion is "; echo -n $z;echo "%";
}
welcome() {
    name="$(uname -a | awk '{print $2}')"
    echo -n "Welcome to the war zone "; echo $name;
    freemem
}

welcome

Pasting this piece of shell code into your .bashrc gives you the greeting message. Feel free to add more functions as per your need.

 

Customized cd command to prompt when you enter any folder which is a git repository 

Before actually customizing the cd command let us first try to build a function that tells us if a folder is a valid git repo or not. The easiest way i could think of was using git status command and waiting for the response, Not a git repository tell us if the folder is a git repo or not.  Then we just utilize the git commands to get desried results. Here is the code

isgit() {
    N="$(git status  2>&1)"
    if [ $? -ne 128 ];
    then  
        N="$(git branch | cut -c3-)"
        commits="$(git rev-list --count HEAD)"
        echo -n "Repository:  ";basename `git rev-parse --show-toplevel`;echo -n "Branch: ";echo  $N; echo -n "Number of Commits:";echo $commits;
    fi
}

Now the only hurdle is how to iverride cd command. bashrc is very powerful even for that, just create a cd() function and call other two functions from it.

 

cd() {
    builtin cd "$@" && isgit;
}

 it first performs the predefined cd command and then runs isgit function.

 

 

 

You can run infinite number of such combinations to get more control and utility of your commands. In the very end, you need to add the following snippet in your .bashrc file to get the discussed results

isgit() {
    N="$(git status  2>&1)"
    if [ $? -ne 128 ];
    then  
        N="$(git branch | cut -c3-)"
        commits="$(git rev-list --count HEAD)"
        echo -n "Repository:  ";basename `git rev-parse --show-toplevel`;echo -n "Branch: ";echo  $N; echo -n "Number of Commits:";echo $commits;
    fi
}

cd() {
    builtin cd "$@" && isgit;
}

ls() {
    builtin ls "$@" && numfiles;
}

freemem() {
    total="$(free -m | awk 'FNR == 2 {print $2}')"
    freem="$(free -m | awk 'FNR == 2 {print $3}')"
    z=$((freem*100 / total))
    echo -n "RAM consumtion is "; echo -n $z;echo "%";
}
welcome() {
    name="$(uname -a | awk '{print $2}')"
    echo -n "Welcome to the war zone "; echo $name;
    freemem
}

welcome

 

We shall continue in next posts with even more  customizations, till the time keep playing with your machines.
#BASHing