Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I currently have this alias:

alias cmakerel='cmake -DCMAKE_BUILD_TYPE=Release -Wno-dev ../../ && make -j4'

Essentially, it will call cmake to do an out-of-source build from PWD. It works fine, but as-is, it can be run from nearly anywhere (which I don't want).

How do I modify this alias so that it runs if and only if the string bld is in my present working directory?

I need this change because without it, I sometimes accidentally invoke this command from the tst directory or some other directory where I just cause a total mess.

share|improve this question
up vote 8 down vote accepted

From man bash:

For almost every purpose, aliases are superseded by shell functions.

So make it a shell function.

function cmakerel {
    if expr match "$PWD" '.*bld.*' >/dev/null ; then
        cmake -D....
    else
        echo "Wrong directory!"
    fi
}

It's a regular expression you can adjust to your needs.

share|improve this answer
    
Great tip! I would give you +5 if I could. TYVM. – kfmfe04 Dec 16 '11 at 21:51
    
btw, how should I modify cmakerel above to return some failure value so when I call cmakerel && make check the second part doesn't get called on failure? – kfmfe04 Dec 25 '11 at 20:12
2  
@kfmfe04 Add a line exit $? below the cmake call. It will then exit with the exit code of the last command performed. Unless you use pipes, it's straightforward. Below the echo call, add exit 1. – Daniel Beck Dec 25 '11 at 20:29
    
great - thx! Add this as an answer to the Question I just posted and so I can send you some points. – kfmfe04 Dec 25 '11 at 20:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .