| « Today's roads, tomorrow | It's official » |
Thu, Sep 15, 2011
![[Icon]](rsc/img/chain_link.gif)
When you have multiple checkouts of a codebase, and work in multiple terminals, it's easy to suffer a momentary confusion about where you're executing a command, and break something important when you thought you were working in a safe place.
This is something I've been thinking about fixing for a while, but then two of my co-workers asked me to come up with something for this issue as well, so I finally got off my posterior and did something that (IMHO) is actually quite cool :)
So what I now have is a bash prompt that looks in your current directory, and also all parent directories, for a file named .ps1_alert and if it finds one (or more) it puts the alert in that file into your prompt, in bright red.
All you have to do is add \[\033[01;31m\]$(__ps1_alerts "(%s)")\[\033[00m\] to your bash prompt. This is easy:
$ echo $PS1
\n\u@\h:\W\$
$ export PS1='\n\u@\h:\W\[\033[01;31m\]$(__ps1_alerts "(%s)")\[\033[00m\]\$ '
$ echo $PS1
\n\u@\h:\W\[\033[01;31m\]$(__ps1_alerts "(%s)")\[\033[00m\]\$This works instantly, but only for the current shell: Add the code to your PS1 in your .bashrc file to make it stick.
Then just save this perl script as /usr/bin/__ps1_alerts and the prompt will start to work:
#!/usr/bin/perl
use strict;
use warnings;
# Get the current directory
my $pwd = `pwd`;
chomp $pwd;
# Populate an array with the directory pathnames
my @dirs = split m#/#, $pwd;
# We only want the 'nearest' alert line, so declare it outside the loop
# and over-write it each time a new one is found
my $alert = '';
# Check for an alert file in each directory from root to current dir.
for my $i (0..$#dirs){
my $wd = join "/", map $dirs[$_], 0..$i;
my $alert_file = $wd.'/.ps1_alert';
# If there's an alert file, grab the first line & output it
if (-e $alert_file && -r $alert_file){
open (my $file, "<$alert_file") or die "Can't open alert file\n";
$alert = <$file>;
chomp $alert;
}
};
# Output the alert:
print $alert;And now, put a .ps1_alert file in anywhere you need it, and you're away.
I think this is cool :o)
![[Links]](http://geekblog.oneandoneis2.org/skins/112/rsc/img/chain_link.gif)
I'm in the Perl newsletter again. I should try and write about some other language...
21/05/12
Facebook Syndication Error
22/05/12
![]()
I last listened to:
Johann Pachelbel - Canon in D major
Most recent photo:
js.js