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)