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 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...