简体   繁体   English

将 rlogin 命令转换为别名

[英]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.我想要一个可以使我的 rlogin 命令更简单的别名。

So, I want to convert following command into an alias :所以,我想将以下命令转换为别名:

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

I tried writing this in my .cshrc file :我试着在我的 .cshrc 文件中写这个:

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

when I do rl 13当我做rl 13

It says :它说:

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

What am I missing here ?我在这里错过了什么?

You can easily see the alias is not defined as intended, by running alias rl :通过运行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 .其次,您需要使用\\!*而不是$1

So you get:所以你得到:

% alias rl rlogin '`echo machine\!* | tr -d " "`'
% alias rl
rlogin `echo machine!* | tr -d " "`

The tr pipeline in your alias is useless.您别名中的tr管道没用。 Simply saying:简单地说:

alias rl 'rlogin machine\!:1'

and invoking it by saying:并通过以下方式调用它:

rl 42

should work, ie should issue rlogin machine42 .应该可以工作,即应该发出rlogin machine42

Note that you need to escape the !请注意,您需要转义! in order to prevent it from being interpreted by the shell for history expansion.为了防止它被 shell 解释为历史扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM