如何从zsh shell的ruby代码中调用别名? [英] How do you invoke alias from ruby code for zsh shell?

查看:126
本文介绍了如何从zsh shell的ruby代码中调用别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从ruby代码中调用别名,以便测试以编程方式插入点文件中的别名。例如,别名如下:

I would like to invoke alias from ruby code so as to test the alias which I programmatically inserted into the dotfile. Say for example, the alias is the following:

alias something="echo somethingelse"

我在网上搜索并找到了针对bash的解决方案:

I searched the web and found the solution for bash:

  #solution for bash
   system %(
     source ~/.bash_profile
     shopt -s expand_aliases
     something
   )

但是,这不适用于zsh。

However, this does not work for zsh.

我尝试使用以下代码(以及其他命令的组合)来调用别名,但无济于事。

I tried to invoke the alias using the following code (and a combination of other commands) but to no avail.

  system %(
    exec zsh #this seems to source .zshrc 
    something #this does not work
  )

我也希望它也适用于zsh。我如何才能在zsh上使用它?有人有建议吗?提前致谢!

I would like it to work for zsh too. How can I get it working for zsh? Does anyone have a suggestion? Thanks in advance!

我也测试了以下内容,但它们不起作用:

I have also tested the following but they don't work:

  system %(
     # exec /bin/zsh #this causes the subsequent lines to not run. 
     source ~/.zshrc #this causes the error lines to be printed
     # setopt aliases #don't think it helps
     something #trying to invoke this which is already in zshrc   
  )

错误消息:


  • /Users/ytbryan/.zprezto/init.zsh:第14行:autoload:命令找不到

  • /Users/ytbryan/.zprezto/init.zsh:第15行:print:命令未找到

  • /Users/ytbryan/.zshrc:第42行: @:不是有效的标识符

推荐答案

一种方法是通过 zsh -c 运行Zsh代码。从 zsh -c 运行时,别名不会扩展,但是内置的 aliases 数组仍然可以访问,因此仍然可以通过手动从数组中检索扩展并手动执行单词拆分来扩展别名。这应该涵盖大多数常见的别名。对于更高级的别名(涉及进程替换,参数扩展,命令替换,算术扩展,花括号扩展,文件名扩展或文件名生成,或者不仅仅是一个简单的命令),可能需要使用 eval (但使用 eval 时需要非常谨慎,并且当输入来自不受信任的来源或受信任但受信任的来源时永远不要使用它)

One approach is to run Zsh code via zsh -c. Aliases are not expanded when run from zsh -c, but the builtin aliases array is still accessible, so one can still expand aliases by manually retrieving expansions from the array and manually performing word splitting. This should cover most of the commonly seen aliases. For more advanced aliases (that involves process substitution, parameter expansion, command substitution, arithmetic expansion, brace expansion, filename expansion or filename generation, or that is more than a simple command), one might need to use eval (but one needs to be very cautious when using eval, and never ever use it when input comes from an untrusted source, or from a trusted but possibly tempered-with source).

可以嵌入到Ruby system 调用中的示例代码:

Example code that could be embedded in Ruby system calls:

> zsh -c 'alias foo="print bar"; ${=aliases[foo]}'
bar
> zsh -c 'alias foo=print; ${=aliases[foo]} $@' -- 1 2 3
1 2 3
> zsh -c 'alias foo="print a b c | grep -o a"; ${=aliases[foo]}'  # simple case where naive expansion fails
a b c | grep -o a
> zsh -c 'alias foo="print a b c | grep -o a"; eval "$aliases[foo]"'  # eval comes to rescue, but be extra careful
a

请注意, source zsh -c 中有效,因此上面的别名定义可以从任何文件中获取而已

Note that source works in zsh -c, so the alias definitions above could be sourced from any file just fine.

这篇关于如何从zsh shell的ruby代码中调用别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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