Monday, October 04, 2010

Techie Tidbits: Resolving Symlinks

If you use a linux distro that uses the alternatives framework, from time to time you might need to know where or what those symlinks eventually point to; this was the case for me when I needed to know where my JRE home was... and which java wasn't helping me (much). Google introduced me to readlink which has an -e or -f option which basically switches on recursion. So something like readlink -f `which java` told me what I needed to know. I went a step further and created two aliases in my ~/.bash_aliases file (sourced from ~/.bashrc), like so:
alias rle='readlink -e'
alias rlf='readlink -f'
So rlf `which java` saves you 7 key presses. All things considered the speed up is probably going to be marginal, but everything helps right?

Techie Tidbits: Beef up the CMD Prompt

Are you a frequent Cygwin user and/or often ask yourself "Why is the CMD prompt so crap?!"? If so, you'll probably be interested to know how you can inject some or possibly all of that Linux/Bash goodness into your CMD prompt.
1st, Install Cygwin if you haven't already done so
2nd, Extend Your PATH
Create a bin (short for binary, but conventionally the home for anything executable) folder in your user directory, then add this directory to your PATH variable: Win+Pause > Advanced Settings > Environment Variables, probably best to add it to User variables for <You>
3rd, Create Your Associations
I've made an associations.cmd file in my personal bin directory with the following contents:
@echo off

assoc .sh=BashScript
ftype BashScript="C:\Cygwin\bin\env.exe" "CYGWIN=nodosfilewarning" "/bin/bash" -l "%%1" %%*

assoc .pl=PerlScript
ftype PerlScript="C:\Cygwin\bin\env.exe" "CYGWIN=nodosfilewarning" "/bin/perl" "%%1" %%*

assoc .py=PythonScript
ftype PythonScript="C:\Dev\Python\2.6(x86)\python.exe" "%%1" %%*

assoc .rb=RubyScript
ftype RubyScript="C:\Dev\Ruby\1.8.6\bin\ruby.exe" "%%1" %%*
Clearly I have Python and Ruby installed on Windows - if you don't omit the last and second to last command pairs. If you have installed the Perl package in Cygwin (which I recommend you do), then the second pair of commands wires that up. Now run this CMD file.
4th, Extend Your PATHEXT
Follow the instructions in step 2, but instead of changing the PATH variable, add the Bash, Python and Perl script file extensions (SH, PY and PL, respectively) to the semi-colon separated PATHEXT variable

This now allows you to run any Bash, Python or Perl script from anywhere on the CMD Prompt, JOY!

A Perl Example
Create prename.pl in your personal bin directory with the following contents:
#!/usr/bin/perl -w

$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = ) unless @ARGV;
for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
}

Now if you have a file called file_123.txt and you wanted to rename it to file_abc.txt, you could run the following in a CMD Prompt:

prename "s,\d+,abc," file*

Techie Tidbits: Get Going with Cygwin

Installing Cygwin is a pretty straight forward exercise - but here I walk-through a few extra configuration steps that make Cygwin a real productivity catalyst for my day-to-day activities.
1st, Install Cygwin
Download the setup.exe file from here. I usually install to C:\Cygwin and stick all the package stuff in C:\Cygwin\.pkg, where I also stick the setup.exe, once I've initially finished with it.
2nd, relocate /home
Following any install or update, I always start up a shell and remove the /home directory and symlink to C:\Documents and Settings (this some how still works on Win 7, but we could easily use C:\Users):
cd /
rm -fr home
ln -s "C:\Documents and Settings" home
This now relieves you of having to maintain two home directories - after a while of using Cygwin you'll start to wonder why they didn't do this automatically.
3rd, add some Bash Here action
Cygwin has a wealth of command-line tools for text processing; that's great - I find these immensely useful when tweaking configuration files or batch processing server log files, for Apache say. The annoying bit is getting a shell open in the directory where you need to go to work: cue Chere!

In the Select Packages screen, search for and install both chere and mintty packages. If you happen to be reading this through once first, you can go ahead and do this step when you first install Cygwin (in step 1).

Now fire up a shell and enter the following command;

chere -ia1 -t mintty -s bash -e 'Bash Here'
Now you should be able to right click on any directory in Windows Explorer and have a Bash Here option
4th, spin up SSHD (skip this if you're asking 'What's SSHD?')
Errm, actually, I'll save this for another blog...

Friday, April 23, 2010

Darren on Ruby: Adding arrays like vectors in Ruby

In the process of writing some RSpec tests, I found it useful to be able to add arrays of numbers together as you would vectors. Of course Ruby has a Vector class, but I was intrigued as to how I might implement such a thing and what extra utility I could provide; I came up with the following:
module Enumerable
  def **(other)
    zip(other.fill(0, other.size, size - other.size)).collect {|a| a.inject {|sum,e| sum+e}}
  end
  
  def vector_add *others
    sums = nil
    if block_given?
      each do |e|
        sums = sums.nil? ? yield(e) : sums ** yield(e)
      end
    else
      sums = self
    end
    
    others.each do |other|
      sums = sums.nil? ? other : sums ** other
    end if not others.empty?
    
    return sums
  end
end
As you can see I have overridden the ** (exponentiation) operator; not sure this is a great idea, that is perhaps violating some 'behavioral integrity', but it can always be changed or replaced. Try running the above and following code (try pasting into IRB)...
puts [1,2,3] ** [4,5,6]
puts [1,2,3,4] ** [4,5,6]
puts [1,2,3].vector_add([4,5,6])
puts [1,2,3,4].vector_add {|e| [e*1, e*2, e*3, e*4]}
puts [1,2,3,4].vector_add([4,5,6]) {|e| [e*1, e*2, e*3, e*4]}
You should get something like the following:
5
7
9
5
7
9
4
5
7
9
10
20
30
40
14
25
36
40
I'm pretty pleased with that. Now, back to RSpec...

Thursday, March 18, 2010

Techie Tidbits - Making Life a Little Easier

Every now and then I come across a wonderful solution to problems I never really realised I had. I've decided to post them all here so a) I can externalise my memory of them and b) Google can do its thing and share the wonderfulness with those that go looking. I'll try to treat this opening blog as an catalog linking to other posts with the details - enjoy q(^_^)p