Parse config files on Linux with Shell Script

Story is, I was looking for a script to parse a config file so I’d be able to read the values after “=” only, skipping empty lines and comments (Lines starting with #). The solutions I found online happened to be too complicated since this task isn’t that hard to carry out. I’ve come to a simple and neat solution which may help a lot of people out there given the amount of lads asking how to do it around the Internet.

First off, let’s assume your config file is called myconfig.conf and it looks something like this:

# My config file
# myconfig.conf

VARIABLE1=anything
VARIABLE2=/somewhere/in/your/disk

Now, to your script: It will read through you config file, line by line, skipping empty lines and ignoring comments. Whenever it finds a line starting with a variable name followed by “=”, it will assume that value as valid. Let’s see how it’ll look like:

# My script
# Config file parser

CONFIG_FILE=/path/to/file/myconfig.conf
IFS="="   #Everything after "=" will be got as a value

analyse_line()
{
    # Parse config file lines
    # Ignore empty lines and comments
     if [ -z "$VALUE" ]; then
         echo "$VALUE" > /dev/null
     # Do something else if the value is valid
     else
         # Just printing the value to see if it's correct
         # You may remove it later
         echo "$VALUE"
         # Do your stuff with VALUE
     fi
}

### Read through the entire file, line by line
while read -r name LINE
do
    VALUE=`echo "${LINE//\"/}"`
    analyse_line
done < $CONFIG_FILE

I hope you find this useful.
Cheers!

Installing CadSoft EAGLE PCB on Linux

If you’ve tried to install the latest Eagle version (6.5) downloaded from CadSoft website, you certainly have found some errors, saying that there are “missing” libraries (Something like this: /tmp/eagle-setup.15133/eagle-6.5.0/bin/eagle: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory). This happens because Eagle looks for libssl.so.1.0.0 and libcrypto.so.1.0.0 in your lib folder, but you’re distro probably doesn’t have those versions of the libs. There must be newer ones instead. The easiest trick to solve this issue is to create a symbolic link to your libs and naming them after the libs Eagle is looking for. In my case, my libs version were:

libssl.so.1.0.1e
libcrypto.so.1.0.1e

I just had to access my /usr/lib folder and run:

sudo ln -s libssl.so.1.0.1e libssl.so.1.0.0

then

sudo ln -s libcrypto.so.1.0.1e libcrypto.so.1.0.0

Later on, Eagle setup process worked like a charm! Hope it helps you too!

Generating (efficient) anagrams in Python

If you’ve read my previous post about permutation in Python (here), you’ve seen that I used a code to generate an anagram from any given word to test the technique. It is a very simple code, but because the way the code works, it brings up some memory issues. The code generates every bloody possible combination of the given word and joins it into a list. Then it picks up one of the results randomly and prints to the user. Can you imagine the size of the list for a 10-character word? Yeah… I know. Dumb!😀

So, here it comes, my improved version of the anagram in Python. First off, to the code:

# It generates a non-repetitive random sequence
# and uses this sequence to form an anagram
# from the given word
def form_anagram2(word):
    # Convert a string into a list
    l = [l.strip() for l in word]
    # Fill the anagram with zeros (character)
    # and the list has a fixed size defined
    # by the size of the word entered as parameter
    anagram = ['0'] * len(l)
    #Form the non-repetitve sequence
    random_range = random.sample(xrange(len(l)), len(l))
    #Fill up the anagram list with the random combination
    for i in range(0, len(l)):
        anagram[i] = l[random_range[i]]
    formed_anagram = ""
    #Join the anagram into a string and return it
    return formed_anagram.join(anagram)

Based on comments, it’s easy to figure out how it works. It splits the given word into a list; generates a non-repetitive random numerical sequence and then fill up the anagram with the letters scrambled, using the random range as the index. The function returns the anagram joined (in order to turn the list into a string) and that’s it!

I hope you like it!

Permutations in Python

WARNING: If you’re reading this searching for a solution on permutations in Python, please follow this link to a newer a better code!😉

What’s the story, lads?
Earlier today I was studying Python alongside a friend and he came up with a question: Can we use this to generate an anagram from a given word? At first sight, it seemed harder than it really is, but with a little bit of research online, I figured out how to achieve this using the itertools module.

It’s an easy trick, where you .join() the result of your permutation into a list. Right after that, you randomly pick one of the members of the list and print it out. Piece of cake, isn’t it? Now, to the code:

import random, itertools

def form_anagram(word):
anagram_list = [''.join(anagram) for anagram in itertools.permutations(word)]
return anagram_list[random.randint(0, len(anagram_list))]

word = raw_input("Type a word to get an anagram: ")
anagram = form_anagram(word)
print "The formed anagram is %s" % anagram

Customising vim

Yaaaah, what’s the story, lads?

So, I was chatting with a friend the other day and he was complaining about some people liking command line, that it was some kind of technology retrocession, using and configuring software, services, etc. His point is that the graphical interface should be developed thinking of the administrator and the power user, not only the regular user. Well, that is, somewhat, good. Very unlikely to happen though.
Linux already has some shiny power graphical tools to ease, replace and mask tasks which we usually perform on the shell, OS X also has something similar, but Windows will hardly adopt this type of philosophy. There is PowerShell, but… let’s not talk about this, ok?😛

He ended up using vim as an example of old and not-state-of-the-art editor. Well, you could say old, but deffo GOLD!
Vim is one the most powerful editors around, in case you haven’t heard of it. It actualy has as many features as a heavy memory gobbler such as Eclipse, for example. Way better: It’s flipping light, pure command line stuff.

At first sight, you might think: ugly, cranky, not-intuitive-at-all, etc. Well, it really looks like that, but with a couple o minutes (yes, minutes) and some good mood, you’ll get there and you’ll love it.
Once you get used to its keyboard shortcuts and options, it’s going to become the hell of an editor. Also you’ll produce much more with vim since the options set on keyboard are done faster than shifting your mouse pointer away on dozens of menus.

I’m gonna leave an example of a simple, but very useful, vim config file, which I use and wrote myself, but you can customise it as much as you like. There are lots of information around the internet, so don’t be afraid.
If you don’t have a .vimrc in your home folder, create one: touch ~/.vimrc

Then, be bold and edit it on vim: vim .vimrc

Press “i” to enter insert mode and paste the text below (Text after ” is comment):

set incsearch     "Set incremental search
set ignorecase     "Ignore case when searching
set scrolloff=4     "Keep 4 lines at bottom and top on the screen
set nobackup     "Get rid of the ~files (backups)
set wildmenu     "Show autocomplete menus
set wildmode=list:longest     "Make wildmenu behave like bash completion.
set shiftwidth=4
set tabstop=4    "Setting identation to 4, using hard tab
set autoindent "Set autoidentation
set cindent "Set autoidentation for C code blocks

ESC to go back to the menu, type: :wq in order to save the file and quit.
Next time you run vim, your new settings will be up.

See you in space, cowboy!

void ThoughtsOnLinuxGamingAndOpenSource

Hey, you!
Don’t worry about my absence cause here I come with some void thoughts (Aha! Finally a joke with the blog’s title) on some… stuff. Stuff to make you think, just in case you’re lazy (or dumb) enough not to do that quite often.
I just came across the Humble Bundle 7 (here!!!). If you don’t know what the Humble Bundle is, it’s a selection of indie (and sometimes, mainstream) games that are sold to you for the price you think they worth. Yeah, that’s it! You pay what you wanna pay!

I got the Humble Bundle 2 and it was nice, but back in that time, there was only one of them running on Linux, so I made it for the charity’s sake of things. It gave me a 50% off on the upcoming Left4Dead 2 on Steam, which I didn’t use, so it expired. Screw me, right?

Actually, what I wanna say here is: Linux gaming is getting there! It’s becoming a bigger and stronger monster, spitting in some people’s eyes all around the globe, showing that WE CAN!
I’m not being an annoying fan-boy whatsoever and I’ll prove it. When Valve announced Steam for Linux, I thought “Cool, man! But… look… it may not work out. I mean, how many people using Linux will buy games?” Oh dear, how wrong was I? Very wrong. Not even Linux users are buying games, but Valve is putting more and more effort on new releases. Left4Dead 2 has sold as fuck for Linux Gamers, so has Counter Strike! (I got CS myself! Fun shit when I’m bored, hehe)

Then I just saw on Humble Bundle’s website some very curious statistics which I’d like to share with you:

Average offer, sorted by OS

Average offer, sorted by OS

As you can see above, Linux gamers have contributed with higher amounts of money than Windows and Mac users.

In the chart below, you’ll see something even more curious:

Top Contributors!

Top Contributors!

I totally recommend you guys to check out LinuxGamers twitter to have a better picture of what I’m talking about: https://twitter.com/@LinuxGamers

We are here, dude! We want to pay for games simply cause we want to PLAY them!

So, do not bow to the big guys! Look for alternatives. Use open source software. Do not believe in that crap that if it’s expensive, it’s good! Apple is out there to prove I’m right!😛
Stop being lazy and feeding stupid thoughts such as “I can’t game on Linux” or “Linux is hard” or “I can’t have my favourite software there”. People who say that are usually the ones who surf on internet and chat on Facebook 90% of the time. Linux is easier to use out-of-the-box than any other OS in the market. Maybe MacOS has it their way, but that’s another story for another time…

I have been using Linux as my main OS for almost 5 years now and I can’t live without it anymore.

We are living in times of rebellions everywhere, wishes of breaking free and fighting the power. Do not take it for granted! You have the choice, so… CHOOSE TO BE FREE!

Have a nice week!😉