BashQL: Another half-assed project by me


Just the Facts

BashQL is a stupid little python script that lets you perform SQL queries on the output of shell commands.

Use case and example:

I wanted to compare what was actually installed by my package manager (pacman -Q) against all the subdirectories where I create the packages for installation (ls -1 /pack):

jason [ ~/Projects/bashql ]$ ./bashql "select * from [ls -1 /pack] where filename not in (select col0 from [pacman -Q])"
filename
---------------------
cairo-compmgr
cdm
cw
dhcp
[...snip...]

Rationale

Granted, for any given single comparison or task like this, it’s easy enough to whip up a short script in whatever language you choose. But, if you already know SQL this has the advantage of using that syntax along with “generic-izing” the problem.

Rambling

The only thing you need is python, because it should include sqlite – which can create a database in memory – so you get everything you need there. Assuming you already know SQL, all you have to do is put the commands in square brackets and the SQL command in quotes. Tables and columns are named as tableX and colX where X starts at 0.

You can create a [command_name].bashql file to tweak parsing the output, and name columns. Note that it is impossible to cleanly parse ls, so don’t get all up in my nose about that. Use ls -1 and don’t have newline characters in your filenames and you should be fine.

This is just a little tool I threw together, so it’s not robust and there isn’t any error-checking. If it were aimed at anyone but me, it would be people that know SQL well and spend a lot of time on the command line.

Alternatives

There’s a project like this, ShellSQL – which apparently was originally named bashql. It takes a different approach and might be of interest if such things interest you.

It’s obvious that a more robust and non-tacked-on-top solution would be fantastic, but unless I totally missed it, there’s no such animal.

, ,

Comments are closed.