/ tuesdaytooling

Tooling Tuesday - .bashrc : Information from your terminal

This post is thanks to @FXBOY4EVA who reminded me of the power of .bashrc

So what is it?

.bashrc is a shell script that is automatically run when a terminal is opened. In the .bashrc file we can place commonly used commands that will auto start when we start a new terminal.

So how is that useful?

You can have your IP address appear in the terminal, the latest news headlines, weather etc all when you open the terminal.

So how can I make my own?

.bashrc is located in your home directory and to edit it we can use any text editor, in my case I use geany.

Before we start to edit the file make a backup just in case!
Open a new terminal and type the following to backup the file to .bashrc.backup

$ cp .bashrc .bashrc.backup

Extra Step!
If you would like to follow my example to the letter, then you will need to install a few packages. In the terminal type the following...
To install jq a JSON filter
$ sudo apt install jq
To install figlet a tool to create text art
$ sudo apt install figlet
To install feedparser an RSS feed parser for Python3
$ sudo pip3 install feedparser

With the backup made, edit the .bashrc file in your favourite text editor.
We need to scroll all the way down to the bottom of the file, and then add a line to explain what we are doing. This is a comment, ignored by the computer, and there only to help us remember.

# Hacking my .bashrc file with cool stuff!

So what shall we put in there?

Well we can get the latest weather for where we live! Using this handy one line bash command that will go to wttr.in and retrieve it! This is then piped into the head command so that we only see the first 7 lines of the forecast.

curl -s wttr.in | head -n7

Fancy a little political action? Get the latest number of signatures for the #revokearticle50 petition! For this we create a variable called signatures and in there we store the output of running the curl command which will grab the latest signature_count from the json source on the petition website. This is then piped | into jq where we instruct it to search for the signature count information.

signatures="$(curl -s "https://petition.parliament.uk/petitions/241584.json" | jq '.data'.'attributes'.signature_count)"

Then we shall use the figlet command to create some ASCII art for a title to #RevokeArt50

figlet "#RevokeArt50"

And we can now show the live signature count by inserting the $signatures variable into a sentence.

echo "There are "$signatures "signatures for the revoke article 50 petition so far"

The last two lines in the file will be to use figlet to create a BBC News title in ASCII art, and then to call a command called news. A command that does not yet exist! We are going to make a quick application to get the news.
Save the .bashrc file before moving on!!

Get the latest news headlines!

For this part of the build we need to create a new file in our editor / Geany as we shall be writing a little Python code.
In order to get the news headlines (or any other RSS feed) we need to parse an RSS feed from the BBC News website and for that we shall use Python and feedparser.
In the new file we start by instructing Python where to find the Python interpreter.


Then we import the feedparser library.

import feedparser

Then we create an object to save the RSS news feed as a dictionary.

news = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml?edition=uk")

Lastly we use a for loop to iterate over the first 5 news items, printing the title of each news item.

for i in range(5):

Save the file as news in your home directory and now close the editor.
For the next part we need to make the news application executable and for that we need the terminal! Using the following command, we make the news file into an application.

$ chmod +x news

Now give it a test by typing

$ ./news

You should see the five latest headlines pop up!
But we are not done yet, for the final part lets copy the news application so that it is available system wide! For this we need the terminal and we type

$ sudo cp news /usr/bin/news

Once that is done, we are done!

Take it for a test drive!

Close any currently open terminals. Then open a new terminal window and you will see the fruits of your labour!

Happy Hacking!