tags:

views:

429

answers:

3

I have a script that takes a command and executes it on a remote host. It works fine, for example:

$ rexec "ant build_all"

will execute the "ant build_all" command on the remote system (passing it through SSH, etc).

Because I'm lazy, I want to set up an alias for this command (and ultimately, several others), such that, for example, I can just invoke

$ rant build_all

and bash will it will automatically invoke

$ rexec "ant build_all"

I tried doing this with alias, but if I define

alias rant=rexec ant

then any arguments passed to "rant" will just be appended to the end, like so:

$ rant build_all -Dtarget=Win32
(interpreted as:)
$ rexec "ant" build_all -Dtarget=Win32

This fails, because rexec really takes just one argument, and ignores the others.

I could probably do this with a bash wrapper script, but I was wondering if bash had any built-ins for doing this for me, perhaps a named-argument version of alias, or a perl-like quote string command (e.g. qw/ / ), or some such.

A: 

You can do it as a function, not an alias:

function rant { rexec "ant $1"; }

You can call it at the command line like an alias

J.D. Fitz.Gerald
+1  A: 

For all arguments, this will work.

function rant () {
    rexec "ant $*"
}

You may need to adjust the quoting, depending on what arguments you're passing.

Ken Gentle
I used this, but with an important generalization. Thanks!
glamdringlfo
(since I cannot accept my own answer, look below for it)
glamdringlfo
+2  A: 

I ended up using Ken G's answer, but one better: defining rex as a function, like so:

function rex {
    run_remote.sh -c "$*"
}

allowed me to then use rexec in aliases, like this:

alias rant="rex ant"

and still have it wrap the arguments up the way I need them.

I forgot I could use functions like that in bash. This does exactly what I needed, without having to create a wrapper script.

Great tip, thanks!

edit: changed "rexec" to "rex", because I found that my system already had a program called "rexec"

glamdringlfo