tags:

views:

70

answers:

2

I wish to accomplish the following:

If I execute "cd production" on bash prompt, I should go into the directory and a message should be displayed "You are in production", so that the user gets warned.

+4  A: 

You can do it by executing the following in the shell context (e.g., .bashrc).

xcd() {
    if [[ "$1" == "production" ]] ; then
        echo Warning, you are in production.
    fi
    builtin cd $1
}
alias cd=xcd

This creates a function then aliases the cd command to that function. The function itself provides the warning then calls the real cd.

A better solution, however, may be to detect real paths, since the solution you've asked for will give you a false positive for "cd $HOME ; cd production" and false negative for "cd /production/x" (if /production was indeed the danger area).

I would do something like:

#!/bin/bash
export xcd_warn="/home/pax /tmp"
xcd() {
    builtin cd $1

    export xcd_path=$(pwd)
    for i in ${xcd_warn} ; do
        echo ${xcd_path}/ | grep "^${i}/"
        if [[ $? -eq 0 ]] ; then
            echo Warning, you are in ${i}.
        fi
    done
}
alias cd=xcd

which will allow you to configure the top-level danger directories as absolute paths.

paxdiablo
This does what the user asked for, but I think what they *need* is something different; they should care whether they're actually in the production directory, not whether they just issued a 'cd' command to get there -- if they got in via a pushd instead, the data is just as sensitive.
Charles Duffy
you forgot the `k` in xcd()
Mark
@Mark, make sure you add the smiley next time - it took a few seconds of looking for the error before I remembered what xkcd was :-) D'Oh!
paxdiablo
That's a good point, @Charles, although you could just as easily alias the other commands as well. It may be that a combination of our methods would be best (your prompt command with the list of dangerous directories). However, it can never be foolproof because fools are so ingenious. A simple "rm -rf /production/important_stuff" won't be caught by either of our methods. I suspect the better option is to limit access to /production and below with file permissions, only allow competent people near production data and make sure backups are done right.
paxdiablo
you dont need to unalias and re-alias the cd command. Just issue "builtin cd $1" instead.
camh
Thanks, @camh, I've been looking for a more elegant way of doing that. Unalias-it/use-it/alias-it again was so crude.
paxdiablo
+8  A: 

Don't do it that way. :)

What you really want to know isn't whether the user just got into the 'production' directory via a cd command; what you really want to know is if you're modifying production data, and how you got there (cd, pushd, popd, opening a shell from a parent process in that directory already) is irrelevant.

It makes much more sense, then, to have your shell put up an obnoxious warning when you're in the production directory.

function update_prompt() {
  if [[ $PWD =~ /production(/|$) ]] ; then
    PS1="\u@\h \w [WARNING: PRODUCTION] $"
  else
    PS1="\u@\h \w $"
  fi
}
PROMPT_COMMAND=update_prompt

Feel free to replace the strings in question with something much more colorful.

Charles Duffy