Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

up vote 6 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
1  
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.