【问题标题】:Equivalent of chmod +x in RubyRuby 中 chmod +x 的等价物
【发布时间】:2016-06-21 03:55:08
【问题描述】:

在 Linux 中,很容易“只是”将可执行文件添加到文件中,只需输入:

chmod +x <fname>

但是,我没能在 Ruby 的宝石中找到这么简单的东西。当然,也可以进行系统调用,即

system( 'chmod +x' << fname )

但是,我正在寻找更“优雅”的东西。

我使用的是 Ruby 1.8.7。

【问题讨论】:

  • 确实如此。不幸的是,我有一个 Ruby 1.8.7 的搜索选项,它没有它......好吧,1 的价格解决了 2 个问题......
  • 哦 - 我明白了...包含您正在使用的 ruby​​ 版本可能是个好主意(特别是如果它是您的限制之一)
  • 来自Ruby 1.8.7 documentationFileUtils.chmod 0644, '/my/directory/which/contains/my/file'
  • 是的,但是我怎么知道“其他权限”,即我不想更改写/读权限。我可以把它们读进去,按位计算,我猜......

标签: ruby linux file-permissions ruby-1.8.7


【解决方案1】:

您可以使用File.stat 读取当前模式,然后使用掩码按位“或”来实现您想要的。这是一个示例(可以缩短):

current_mask = File.stat('foo.sh').mode new_mask = current_mask | '0000000000000001'.to_i(2) File.chmod(new_mask, 'foo.sh')

【讨论】:

    【解决方案2】:
    require "fileutils"
    
    FileUtils.chmod("+x", "foo.sh")
    

    【讨论】:

      猜你喜欢
      • 2011-02-12
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2010-09-20
      • 1970-01-01
      • 2015-01-07
      • 2021-12-29
      相关资源
      最近更新 更多