Ever thought of boosting your commands on Linux Terminal and making them even more powerful and informative? Well Linux Terminal provides you the power to mold 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.

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

Linux Terminal 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 Linux terminal the content of .bashrc file is 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 predefined command uname -a to get the username. The difficulty here is, along with the username it gives a lot of other information 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 the 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

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 desired 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 override cd command. bashrc is very powerful even for that, just create a cd() function and call the other two functions from it.

 

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

 

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

You can run an 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

 

Linux and Linux Terminal can only be mastered by getting your hands dirty. So pick up your keyboards and play with your machines. Brake them, then try to remake them and you will master the skills one day. In the meanwhile, we shall continue in the next posts with even more customizations.
#BASHing


0 Comments

Leave a Reply

Avatar placeholder

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