simple documentation query tool (CLI)

Using Linux, you may have encountered a lot of different commands and dozens of possible options. In my case, there are some commands, which I use daily, and there are some, which I have never even used once.  I was fed up with remembering names and looking up the options I’ve used; I was and am convinced, that you don’t need to remember everything, especially nowadays with so much information everywhere – it’s rather about how we deal with it. So I started gathering all useful commands in a plain text file at some time. The file looks like the following (please notice, that a chunk of command documentation starts with {commandname} and the empty line between two chunks):

{docker}
 # systemctl start docker                    // to start environment
 $ docker                                    // to display commands
 $ docker run [Image]                        // to start an image
 $ docker run -itd [Image] /bin/bash         // to start image with interactive tty as a deamon + bash
 $ docker run -itP -v /webapp -v ~/Downloads/redmine/redmine-2.6.1-1:/webapp ubuntu /bin/bash // … to create the folder webapp and mount redmine on it
 $ docker exec -it elegant_davinci /bin/bash // to login into a running container
 $ docker stop [ContainterID OR name]
 $ docker ps                                 // to display all active container
 $ docker ps -a                              // to display all container
 $ docker logs [name of container]           // to show sdt-out
 $ docker rm[i]                              // to delete container or container[i]mage
 within docker:
 exit OR Ctrl+D                              // to quit container
 Ctrl + P + Q                                // can't remember anymore...
{tar}
 tar -zxvf file.tar.gz
 tar -jxvf file.tar.bz2
...

At first I was satisfied with displaying the whole file using cat, but as Linux seems to have infinitive useful commands, the file grew longer and longer and the time to retrieve the information I wanted rose. Well of cause I could have used piping with grep, but sometimes I just added some additional notes, which didn’t contain the name of the command, thus grepping the line would be pretty complicated and would take even more time. So, to make things short, I have written a simple python3 script, which only requires a string to look for and returns a whole command chunk with extra notes and everything. Here is an example:

localhost@hostname ~ % awesomescript {pacman}
 {pacman}
 pacman -S
 pacman -Scc //empty cache
 pacman -Syy //update package list
 pacman -Su //System update
 pacman -Qs [package] //Querry search
 useful comment one
 useful comment two

With that my documentation problem was solved. Additionally, I added some more features:

Usage:
 awesomescript [OPTIONS OR SEARCHWORD]
Options:
 awesomescript -l   List all chunks of the documentation file.
 awesomescript -lq  List all chunks of the documentation file and querry by chunk number.
 awesomescript -h   Display this help.

This is le petit script behind it:

#!/usr/bin/python3
import sys

def displayhelp():
    scriptpath = str(sys.argv[0])
    scriptname = scriptpath.split('/')[-1]
    print('Usage:',scriptname, '[OPTIONS OR SEARCHWORD]')
    print('\nOptions:')
    print(scriptname,'-l                List all chunks of the documentation file.')
    print(scriptname,'-lq               List all chunks of the documentation file and querry by chunk number.')
    print(scriptname,'-h                Display this help.')

def querydoc(suche):
    fin = open('/PATH/TO/DOCUMENTATION')
    absatz = 0
    for line in fin:
        line=line.strip()
        if suche in line:
            absatz = 1
        if absatz == 1 and line!='':
            absatz = 1
        if absatz == 1 and line=='':
            absatz = 0
        if absatz == 1:
            print(line)
    print()

def listchunks():
    fin = open('/PATH/TO/DOCUMENTATION')
    listing = list()
    for line in fin:
        line=line.strip()
        if '{' and '}' in line:
            listing.append(line)
    print('Those are the avialable chunks:')
    listing.sort()
    for i in range(len(listing)):
        print(listing[i])

def listchunksandquerry():
    fin = open('/PATH/TO/DOCUMENTATION')
    listing = list()
    for line in fin:
        line=line.strip()
        if '{' and '}' in line:
            listing.append(line)
    print('Those are the available chunks:')
    listing.sort()
    for i in range(len(listing)):
        print(i,listing[i])
    qquery = input('Input a chunk number to querry chunk or hit [Ctrl+C] to leave: ')
    querydoc(listing[int(qquery)])

if len(sys.argv) != 2:
    displayhelp()
else:
    suche = str(sys.argv[1])
    if suche =='-h':
        displayhelp()
    elif suche =='-l':
        listchunks()
    elif suche =='-lq':
        listchunksandquerry()
    else:
        querydoc(suche)

extract everything script

I’m very proud to present my first very useful thing to you: a shell script that automatically extracts some common archive-formats-files to your current directory. I promise to update this script as I encounter new archive-formats!

le script

#!/bin/bash
#this is the super lazy extract-all-you-can-script!
if echo $1 | grep .tar.gz; then
   tar -zxvf $1
   exit 0
fi
if echo $1 | grep .tar.bz2; then
   tar -jxvf $1
   exit 0
fi
if echo $1 | grep .tar.xz; then
   tar -Jxvf $1
   exit 0
fi
if echo $1 | grep .zip; then
   unzip $1
   exit 0
fi
echo "something went wrong :("
exit 1

installation instruction

Copy the script into a file, e.g. ‘yournewveryusefulthing’, and make it executable with ‘chmod +x yournewveryusefulthing‘. Now you can run the script with ‘sh yournewveryusefulthing‘. If you like the CLI as much as I do, you can move the file into your $PATH, for example by moving the script to the /usr/bin/ directory

usage instructions

Now its time to extract! Try ‘sh yournewveryusefulthing myarchivefile‘ or (when script is in $PATH included, only:) ‘yournewveryusefulthing myarchivefile‘ to do wonders!

That is it for today, thank you for reading!