Posts Tagged ‘shell’

2012w01

Sunday, January 8th, 2012

column

The other day I wanted some prettier (tabularized) output and of course someone has already wanted this and of course there are tools for that :)

bash_completion

This is so frakking cool! I’ve built this little shellscript “vault.sh” which is a simple wrapper script for mounting and unmounting encfs mounts.

It takes two parameters: operation and target, where operation can be one of “lock” and “unlock”, and target—at present—resolves to “thunderbird” (signifying my .thunderbird directory).

Since I intend to expand this with more encrypted directories as I see fit, I don’t want to hard-code that.

What I did want, however, was to be able to auto complete operation and target. So I looked around, and found this post, and although I couldn’t derive enough knowledge from it to solve my particular problem, having multiple levels of completion, the author was gracious enough to provide references to where s/he had found the knowledge (here, here and here). That second link was what did it for me.

My /etc/bash_completion.d/vault.sh now looks like this:

_vault()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    first="lock unlock"
    second="thunderbird"

    if [[ ${cur} == * && ${COMP_CWORD} -eq 2 ]] ; then
        COMPREPLY=( $(compgen -W "${second}" -- ${cur}) )
        return 0
    fi

    if [[ ${cur} == * && ${COMP_CWORD} -eq 1 ]] ; then
        COMPREPLY=( $(compgen -W "${first}" -- ${cur}) )
        return 0
    fi
}
complete -F _vault vault.sh

And all the magic is happening in the two if-statements. Essentially: if current word (presently half typed and tabbed) is whatever, and this is the second argument to the command, respond with suggestions taken from the variable $second.

Otherwise, if current word is whatever, and this is the first parameter, take suggestions from the variable $first.

Awsum!

awk for great good

Another great use for awk: viewing selected portions of source code. For instance, in Perl, if you just want to view a specific subroutine, without getting distracted by all the other crud, you could do: $ awk '/sub SomeSubName/,/}/' somePerlModule.pm

Links

If PHP were British, perhaps it’s just me, but I find it hilarious.

PayPal just keeps working their charm…

Belarus just… wait what?

Why we need version control

Preserving space, neat!

Fuzzy string matching in Python

If you aren’t embarrassed by v1.0 you didn’t release it early enough

The makers schedule, oldie but goldie

CSS Media Queries are pretty cool

Static site generator using the shell and awk

A netstat companion

Reducing code nesting

Comparing images using perceptual hashes

Microsofts GPS “avoid ghetto” routing algorithm patent…

2011w30

Sunday, July 31st, 2011

Hacking the shell

I’ve scratched some itches this week, some by pure luck, others by seeking these itches out and scratching them with a vengeance ;)

Added to my ~/.bash_aliases is now alias pwd='pwd -P' which tells pwd to resolve the real path when you have cd’d in to a symlinked directory.

Not really a hack, but I discovered Meta-BackSpc / ESC-BackSpc which will, unlike Ctrl-w, delete parts of a path, instead of the entire path.

This thread was of great help in finding that key combination.

Also, thanks to Rikard I got hold of this document which outlines how I would go about getting a word, after the cursor, deleted. Before the cursor is eash (Ctrl-w), but after has always eluded me. Turns out it is Meta-d (or, as in my case, if that is overridden by your window manager, “ESC d”). VERY nice!

I did update my PS1 variable, both for root and my ordinary user. I don’t really know why I hadn’t thought of adding a time indicator before.

There have been times when I have started a long running process, and half an hour or so into it realized that it would have been nice to time the process. Now, this wouldn’t exactly be able to fully replace time, but could do in a pinch so to speak.

root: PS1='\n\[\e[0;33m\][\t] \[\e[0;31;5m\]\u@\h\e[25m\] \[\e[01;34m\]$(pwd)\n\[\e[0m\]# '

which will create the prompt:

[HH:MM:SS] root@host /present/working/directory
#

(yes, root@host IS blinking. root can haz be seriuz bizness!)

user: PS1='\n\[\e[0;33m\][\t] \[\e[0;37m\]\u@\h \[\e[01;34m\]$(pwd)\[\e[0m\]\n\$ '

and this produces

[HH:MM:SS] patrik@host /present/working/directory
$

ImageMagick and Animations

I kindof just assumed imagemagick would have some sort of way of creating an animated gif, and of course imagemagick didn’t let me down.

What surprised me was how incredibly easy it was.

Duck Duck Go (as I am attempting a google-free week) gave me this result, and once I had tweaked the parameters somewhat I ended up with this: $ convert -delay 50 -loop 0 root*.gif rootshell.gif

Zenity and passtore

I also managed to convince (nagged him until his ears risked falling off ;)) Rikard to try out my password management system passtore.

He did have one counter-demand however: he’d need a GUI for day-to-day operations. Me, being a wmii acolyte, can do without it, but I can see how he would feel different, and seeing as it would be a fun exercise, and I’d get him to give it a shot, I sat down and had at it.

Zenity was my first thought, and as it worked out well, it was my only thought ;) The working name for that code is “Heaven ZENt”. Yes I know, I am a stupid wanker, I can live with that ;D

I am however pondering whether I should release this code as a separate project (it depends on passtore, and is useless without it, which should indicate “no”) or not (it does introduce a dependency on zenity, which isn’t strictly needed to operate passtore).

At present time it is only a wrapper around the getpass script from passtore. In essence, “heaven ZENt” is “wyrd” to “passtore’s” “remind”. I am unsure as how to proceed. Input would, as always, be appreciated.

Musings

I’ve given myself the mission to move one of my (spam-trap) email accounts from Thunderbird to mutt. I really want to be able to use mutt as that would be one less thing I’d need to start X for.

So I am looking at other peoples .muttrc files, and came across the index_format setting. This controls how your list of emails in an “inbox” is presented. In Thunderbird this view (simplified) is “some flags, subject, some more flags, author name, even more flags, date”, and what I realize from the .muttrc files I’m viewing, is that at least some people place the date before author, before subject.

That got me thinking about whether this is some sort of brain hack, to force the brain into working in a different way. Coming from Thunderbird, when a new message arrives in a conversation my reaction is to either open it, or let it be for now, depending on whether it is a discussion I am interested in.

At that point I don’t bother to look at the author field, if the conversation is uninteresting (to me) I will simply defer it to later, and if it is interesting I will simply open the new mail and read it, thereby getting to know who wrote it.

Putting the author in front of the subject could let you filter on people rather than on subject, as we all have some people we listen more intently to than others, so whatever discussion they’re in, it might be worth while to read it.

But that still didn’t explain why they’d put date and time before author. The only idea I have about this is that some mails will be urgent and thus it is good to see when they arrived. But I don’t know. I think I will try out “flags, author, subject, datetime” and see how that goes.

Revelation

If you mess up the datetime settings in BIOS (because you have powered down and unplugged your system during a thunderstorm, AND your motherboard is so old that the CMOS battery is dead) you may experience some problems when booting.

In my particular case, FSCK (or whatever binary it is that is performing a quick check on the file system during boot) reported that the datetime was now 2011-07-11 (because I fucked that up), but the last check had been performed in 2011-07-22.

This made it confused and prompted me to manually repair the file system, by entering the root password (which I have unset, -1 for sudo / me) and doing some operations.

That was actually not such a fun night trying to get to the bottom of that.

:wq

Bash history

Tuesday, March 17th, 2009

I’ve seen this around a couple of other blags, but never thought much of it until today, so here is my “contribution” ;D

$ history|awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|head
119 ls
101 cd
44 hg
29 sudo
26 gvim
22 less
13 rm
10 man
9 mv
8 ll

On my stationary box the same list yields:

104 ls
73 hg
69 cd
49 make
25 gvim
20 rm
20 ipython
19 sudo
18 less
11 echo

I however have a feeling that Ubuntu truncates the .bash_history file, because I suspect I have opened gvim more than 25 times in the past week, let alone since system install. They eyebrow raiser here should be “echo”, since I only use that once in awhile to truncate files (i.e. echo “” > afile).

All in all, the tell-tales are all there, GVim, Python and Mercurial are my “weapons of choice”. ;D