你如何运行pushd和popd使用反引号?
每当我pushd /tmp用反引号运行时,我都会收到错误:
"No such file or directory - pushd /tmp"
Run Code Online (Sandbox Code Playgroud)
Car*_*ter 10
Ruby shell-out(反引号)每个都在一个新的子shell中运行,所以它可能不像你想的那样工作:
a = `pwd`
`cd '/tmp'`
b = `pwd`
b == a # => true
b == "/tmp" # => false
Run Code Online (Sandbox Code Playgroud)
另外,你确定pushd在你的shell中工作吗?也许看一下使用ruby's system或者popen3你想要一些比反引号语法更有用的东西.
Dir#chdir接受一个块.以下是文档中的示例,如果您只需要在目录中运行某些命令然后更改回来:
Dir.chdir("/var/spool/mail")
puts Dir.pwd
Dir.chdir("/tmp") do
puts Dir.pwd
Dir.chdir("/usr") do
puts Dir.pwd
end
puts Dir.pwd
end
puts Dir.pwd
Run Code Online (Sandbox Code Playgroud)
您不能以这种方式使用带有反引号的pushd;pushd是 Bash 内置程序,而不是可执行文件。但是,您可以使用 Ruby Shell模块获得类似的功能。
require 'shell'
shell = Shell.new
shell.pushd '/tmp'
shell.popd
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1722 次 |
| 最近记录: |