在另一个别名中新定义的别名,第一次执行将失败 [英] A newly defined alias within another alias, the first excution will fail

查看:131
本文介绍了在另一个别名中新定义的别名,第一次执行将失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们在另一个别名A中定义和使用别名B,则第一次执行A将失败。例如,

  alias A =’alias B = which ls; B;'

第一次执行看起来像(bash中)

  bash:B:未找到命令

上面的示例是一个简化的构造。在实践中,我们可能会隐式地满足这种用法。



原因可能是:当我们执行别名A时,所有扩展都在执行之前执行,但是当我们第一次执行A时未定义B。 p>

那么有没有办法使首次执行成功?



我遇到的情况比上面更复杂。我的别名看起来像(在tcsh中)



别名A’cmd1; cmd2; B -v arg_of_B; cmd3; cmd4;’



其中,B是cmd2定义的别名。另外,B的定义看起来像(在tcsh中)



别名B'source / path / to /script.csh'



因此,使用eval推迟执行B可能不起作用,因为eval将派生一个新的shell以执行命令。使得情况变得更复杂的另一个因素是别名后面有参数。我尝试使用exec,但未成功。

解决方案

问题可以简化为简单的别名。以下也是第一次失败:

  alias A = ls; A 

要推迟扩展,可以使用 eval

 别名A = df;评估A 

(在bash和tcsh中测试)。


If we define and use an alias B within another alias A, the first time execution of A will fail. For example,

alias A='alias B="which ls"; B;'

The first time excution would look like (in bash)

bash: B: command not found

The example above is a simplified construction. in practice, we might meet such usage implicitly.

The reason might be: when we execute the alias A, all expansions are carried out before execution, but B is undefined when we execute A for the first time.

So is there a way to make the first time execution successful?

The situation I met is more complex than above. My alias looks like (in tcsh)

alias A 'cmd1; cmd2; B -v arg_of_B; cmd3; cmd4;'

where, B is an alias defined by cmd2. In addition, the definition of B looks like (in tcsh)

alias B 'source /path/to/script.csh'

So using eval to postpone the execution of B might not work, because eval will fork a new shell to execute the command. Another factor making this more complex is there are arguments following the alias. I tried to use exec but without achieving success.

解决方案

The problem can be simplified to a simple alias. The following fails for the first time as well:

alias A=ls ; A

To postpone the expansion, you can use eval:

alias A=df ; eval A

(Tested in bash and tcsh).

这篇关于在另一个别名中新定义的别名,第一次执行将失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆