bash 如何运行 cd 命令/从 ruby​​ 脚本更改文件夹路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19181319/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:10:48  来源:igfitidea点击:

How to run cd command/change the folder path from ruby script

rubybash

提问by Rak

I am trying to run a command from ruby script. I got stuck in changing the folder path. Below is the command that I wrote. Can anyone let me know how to go ahead?

我正在尝试从 ruby​​ 脚本运行命令。我被困在更改文件夹路径中。下面是我写的命令。谁能让我知道如何继续?

system("cd /home/user/Source/pxe/")

I want the terminal to point to the folder pxewhen I run the ruby script. Is the code above correct? If not, can you let me know what the correct way is to call the cdcommand from ruby script?

我希望终端pxe在运行 ruby​​ 脚本时指向该文件夹。上面的代码正确吗?如果没有,您能否让我知道cd从 ruby​​ 脚本调用命令的正确方法是什么?

回答by Sergio Tulentsev

Do you want to change current directory for the script? Use Dir.chdir.

是否要更改脚本的当前目录?使用Dir.chdir

Dir.chdir('/home/user/Source/pxe')

回答by Arup Rakshit

You can also use FileUtils#cdmethod.

您也可以使用FileUtils#cd方法。

Changes the current directory to the directory dir.If this method is called with block, resumes to the old working directory after the block execution finished.

将当前目录切换到目录dir。如果用block调用此方法,则在block执行完成后恢复到旧的工作目录。

Example( I am on windows-7) :

示例(我在 windows-7 上):

require 'fileutils'

Dir.pwd # => "C:/Program Files/Notepad++"
FileUtils.cd("C:\Users\rakshiar\Downloads")
Dir.pwd # => "C:/Users/rakshiar/Downloads"