Posts tagged with “unix”

Sun 3 Oct

Instant Runoff Voting: A bash implementation

I love shell scripts. If I have something simple to write that a normal person would try to do in a scripting language like Perl or Python, I try to do it with bash. And, as much as possible, I try to avoid using fancy stuff like variables, sed, or awk.

I didn't succeed in the latter goal this time, but I did succeed in implementing Instant Runoff Voting as a bash script. It takes as input a file called irv.txt, which lists each ballot (ranked set of choices) on a separate line as a comma-delimited ordered list.

Here's a sample input file, and here's the script itself. It could be much simpler and probably cleaner, but I tried to make it clear what was going on at each step (and demonstrate the power of piped commands).

· Tags: ,

Thu 16 Sep

How to Kill an Unresponsive SSH Session, and other useful escape sequences

I always forget how to do this.

[newline] ~ .

That's enter key, tilda, then period. Presto, back to your friendly (local) console.

Here are a few more useful escape sequences, straight from man ssh (note that all must be preceded by a newline character):

The supported escapes (assuming the default ‘~’) are:

 ~.      Disconnect.

 ~^Z     Background ssh.

 ~#      List forwarded connections.

 ~&      Background ssh at logout when waiting for forwarded connection / X11 sessions to terminate.

 ~?      Display a list of escape characters.

 ~B      Send a BREAK to the remote system (only useful for SSH protocol version 2 and if the peer supports it).

 ~C      Open command line.  Currently this allows the addition of port forwardings using the -L, -R and -D options (see above).  It also allows the cancella‐
         tion of existing remote port-forwardings using -KR[bind_address:]port.  !command allows the user to execute a local command if the PermitLocalCommand
         option is enabled in ssh_config(5).  Basic help is available, using the -h option.

 ~R      Request rekeying of the connection (only useful for SSH protocol version 2 and if the peer supports it).

Nod to this guy, whose site I've always wound up at when I'm trying to remember this.

· Tags: ,

Tue 14 Sep

One-liner to extract a list of link addresses from an HTML file

I'm moving my research group's website to a new server and making some updates at the same time. One of the main things I need to do is make sure links are going to work after the transition. Here is a little one-line shell "script" (if you can call it that) that will extract link addresses from an HTML web page:

wget -q -O - | tr " " "\n" | grep "href" | cut -f2 -d"\""

wget fetches the file and outputs its content to stdout. tr replaces all spaces with newlines, grep filters out every line that doesn't contain an "href", and finally cut displays everything between the first pair of double-quotes.

If you want to use a file you have on your local machine, you can use this variant instead:

tr " " "\n" < [file_name.html]| grep "href" | cut -f2 -d"\""

Obligatory disclaimer: HTML is NOT a regular language and in general cannot be parsed with regex's as is done here. This is not guaranteed to work.

· Tags: , ,