bash 如何指定用于 ruby 系统调用的 shell?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1239510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I specify the shell to use for a ruby system call?
提问by Daniel Vandersluis
I am trying to run commands from ruby via system(or by using backticks), but am running into problems. When I try to call a command, the shell is unable to find it, even though I know it works if I call it straight. For example:
我正在尝试通过system(或使用反引号)从 ruby 运行命令,但遇到了问题。当我尝试调用命令时,shell 无法找到它,即使我知道如果我直接调用它就可以工作。例如:
`zip`
>> sh: zip: command not found
The problem seems to be that ruby is using the shshell, in which $PATHis not set correctly, rather than bash, and I am not sure why. The user my application is running under is set up to use bashby default.
问题似乎是 ruby 正在使用shshell,其中$PATH设置不正确,而不是bash,我不知道为什么。我的应用程序在其下运行的用户设置为bash默认使用。
Is there any way to tell ruby to use bashinstead of sh?
有什么方法可以告诉 ruby 使用bash而不是sh?
采纳答案by Corban Brook
Why not just specify the full path the the zip executable.
为什么不指定 zip 可执行文件的完整路径。
回答by sepp2k
As far as I know, the only way to do that is to explicitly invoke the shell, e.g.
据我所知,唯一的方法是显式调用外壳,例如
`bash -c zip`
or
或者
`#{ ENV['SHELL'] } -c zip`
Or with system: system("bash", "-c", command)
或与系统: system("bash", "-c", command)
However ruby (and all processes spawned by ruby) should inherit the parent processes' environment and thus have $PATH set correctly even when using another shell. Do you maybe run ruby from a cron job or init script or the like, where PATH is simply not set correctly?
然而,ruby(以及由 ruby 产生的所有进程)应该继承父进程的环境,因此即使在使用另一个 shell 时也正确设置了 $PATH。您是否可能从 cron 作业或初始化脚本等运行 ruby,其中 PATH 设置不正确?
回答by Javier
i'm guessing that Ruby uses the system()C function, which uses /bin/sh. In most systems, /bin/shis just a symlink to other shell, so you can change to whatever you want
我猜 Ruby 使用system()C 函数,它使用/bin/sh. 在大多数系统中,/bin/sh只是到其他 shell 的符号链接,因此您可以更改为您想要的任何内容

