2014-07-03 29 views
2

當我從Rails的控制檯運行一個簡單的命令ls,我得到這個錯誤運行shell命令錯誤其他命令如cd ~。任何人都可以告訴我爲什麼當前文件夾的內容不顯示?爲什麼我得到在軌控制檯(IRB)

UPDATE: 試圖exec('ls')後,這是輸出,這讓我覺得這一定是有些地方設置。

irb(main):001:0> exec('ls') 
Errno::ENOENT: No such file or directory - ls 
    from (irb):1:in `exec' 
    from (irb):1 
    from  /[filepath]/runtime/ruby1.9/gems/1.9.1/gems/railties-3.2.18/lib/rails/commands/console.rb:47:in `start' 
from /[filepath]/runtime/ruby1.9/gems/1.9.1/gems/railties-3.2.18/lib/rails/commands/console.rb:8:in `start' 
from /[filepath]/runtime/ruby1.9/gems/1.9.1/gems/railties-3.2.18/lib/rails/commands.rb:41:in `<top (required)>' 
from /[filepath]/runtime/ruby1.9/1.9.1/rubygems/custom_require.rb:36:in `require' 
from /[filepath]/runtime/ruby1.9/1.9.1/rubygems/custom_require.rb:36:in `require' 
from script/rails:6:in `<main>' 
+1

你能夠在Ruby之外運行'ls'嗎? – zetetic

+0

是的,在退出控制檯後,「ls」按預期工作。 –

+1

你看過你的'PATH'環境變量嗎?你有沒有試過使用'ls'的完整路徑? –

回答

1

這是因爲你現在實際上是在上下文中的交互的Ruby會話(請注意您的提示irb)(所以你可以使用這個類,活動記錄模型等)的紅寶石在軌道上應用。你發出的命令應該是ruby命令。原始的shell命令在這裏不起作用。

但是你可以使用EXEC:

$ rails c 
Connecting to database specified by database.yml 
Loading development environment (Rails 3.2.17) 
2.0.0p247 :001 > exec('ls') 
app  config.ru doc  Gemfile.lock log  README.rdoc spec 
config db   Gemfile lib   Rakefile script  tmp 
16:12:10 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker master 
$ 

您還可以使用backicks(`)來運行命令,即

2.0.0p247 :007 > `ls` 
=> "app\nconfig\nconfig.ru\ndb\ndoc\nGemfile\nGemfile.lock\nlib\nlog\nRakefile\nREADME.rdoc\nscript\nspec\ntmp\n" 

而且%x

2.0.0p247 :020 > %x('ls') 
=> "app\nconfig\nconfig.ru\ndb\ndoc\nGemfile\nGemfile.lock\nlib\nlog\nRakefile\nREADME.rdoc\nscript\nspec\ntmp\n" 

system

2.0.0p247 :021 > system("ls") 
app  config.ru doc  Gemfile.lock log  README.rdoc spec 
config db   Gemfile lib   Rakefile script  tmp 
=> true 
+0

請參閱我的更新。我猜測它是一個本地配置或環境問題。 –

+0

似乎他已經在使用反引號 – zetetic

+0

是的,我試過反引號,%x,system和exec。 –

相關問題