为什么我不能从 Ruby 中调用“history”?

发布于 2024-09-24 01:26:56 字数 488 浏览 4 评论 0原文

我可以使用反引号(以及 %x()、系统等)从 Ruby 程序或 irb 运行 Bash shell 命令。但由于某种原因,这不适用于历史。

例如:

jones$ irb --simple-prompt
>> `whoami`
=> "jones\n"
>> `history`
(irb):2: command not found: history
=> ""

在 Ruby 程序中,它会产生以下错误:

/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31: command not found: history

在 bash 本身中,这些命令工作正常

这并不是说 Ruby 调用正在调用新的 shell - 它只是找不到该命令...

有人知道为什么吗?我很困惑...

I can run Bash shell commands from with a Ruby program or irb using backticks (and %x(), system, etc). But that does not work with history for some reason.

For example:

jones$ irb --simple-prompt
>> `whoami`
=> "jones\n"
>> `history`
(irb):2: command not found: history
=> ""

From within a Ruby program it produces this error:

/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31: command not found: history

In bash itself, those commands work fine

It's not that the Ruby call is invoking a new shell - it simply does not find that command...

Anyone know why? I'm stumped...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笨死的猪 2024-10-01 01:26:56

大多数 UNIX 命令都是作为可执行文件实现的,反引号运算符使您能够从脚本中执行这些命令。然而,有些 bash 解释的命令不是可执行文件;它们是 bash 命令本身内置的功能。 history 就是这样一个命令。执行此命令的唯一方法是首先执行 bash,然后要求它运行该命令。

您可以使用命令 type 告诉您特定命令的类型,以便了解是否可以从 ruby​​(或 python、perl、Tcl 等脚本)执行它。例如:

$ type history
history is a shell builtin
$ type cat
cat is /bin/cat

您还会发现您也无法执行 .bashrc 文件中定义的别名,因为它们也不是可执行文件。

请记住,执行命令并不意味着“运行此 shell 命令”,而是“运行此可执行文件”。如果它不是可执行文件,则无法执行它。

Most unix commands are implemented as executable files, and the backtick operator gives you the ability to execute these commands from within your script. However, some commands that are interpreted by bash are not executable files; they are features built-in to the bash command itself. history is one such command. The only way to execute this command is to first execute bash, then ask it to run that command.

You can use the command type to tell you the type of a particular command in order to know if you can exec it from a ruby (or python, perl, Tcl, etc script). For example:

$ type history
history is a shell builtin
$ type cat
cat is /bin/cat

You'll also find that you can't exec aliases defined in your .bashrc file either, since those aren't executable files either.

It helps to remember that exec'ing a command doesn't mean "run this shell command" but rather "run this executable file". If it's not an executable file, you can't exec it.

情愿 2024-10-01 01:26:56

这是一个内置的。一般来说,您可以通过手动调用 shell 来运行内置程序:

`bash -c 'history'`

但是,在这种情况下,这可能没有用。

It's a built-in. In general, you can run built-ins by manually calling the shell:

`bash -c 'history'`

However, in this case, that will probably not be useful.

愚人国度 2024-10-01 01:26:56
{~} ∴ which history
history: shell built-in command
{~} ∴ which history
history: shell built-in command
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文