« Today's roads, tomorrowIt's official »

Thu, Sep 15, 2011


• Post categories: Omni, FOSS, Technology, Programming

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
$ 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:


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)

No feedback yet


[Links][icon] My links

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist


[FSF Associate Member]

February 2017
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28          


User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by b2evolution