Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a rlogin command into an alias

I work on multiple machines and need to login into them remotely. I want to have an alias which can make my rlogin command simpler.

So, I want to convert following command into an alias :

rlogin `echo "machine $num" | tr -d ' '`

I tried writing this in my .cshrc file :

alias rl rlogin `echo machine$1| tr -d ' '`

when I do rl 13

It says :

usage: rlogin [ -8EL] [-e char] [ -l username ] host

What am I missing here ?

like image 304
nav_jan Avatar asked Dec 12 '22 08:12

nav_jan


2 Answers

You can easily see the alias is not defined as intended, by running alias rl:

% alias rl rlogin `echo machine$1| tr -d ' '`
% alias rl
rlogin machine

There are two problems with the way you define your alias.

First, when defining an alias like this:

alias rl rlogin `...`

the ... command is evaluated at the time the alias is defined, while you need to defer the evaluation to the time it is used. The correct way to do it is to encapsulate everything in single quotes, like

'`...`'

(Also, we need to replace the internal single-quotes with double-quotes, in order not to clash with the outer single-quotes).

Second, you need to use \!* instead of $1.

So you get:

% alias rl rlogin '`echo machine\!* | tr -d " "`'
% alias rl
rlogin `echo machine!* | tr -d " "`
like image 197
shx2 Avatar answered Jan 22 '23 13:01

shx2


The tr pipeline in your alias is useless. Simply saying:

alias rl 'rlogin machine\!:1'

and invoking it by saying:

rl 42

should work, i.e. should issue rlogin machine42.

Note that you need to escape the ! in order to prevent it from being interpreted by the shell for history expansion.

like image 39
devnull Avatar answered Jan 22 '23 13:01

devnull