3
mount
    /project on /mount_1 type none (rw,bind)
    /project on /mount_2 type none (rw,bind)
    /project on /mount_3 type none (rw,bind)

如何用 ruby​​(不是 shell !!)检查某些目录是否安装在 /mount_X 上?

有什么比打开 /proc/mounts 并在那里寻找 /mount_X 更容易的吗?

4

4 回答 4

1

另一种方法是:

system("mount|grep /mount_X")
于 2011-11-23T17:48:49.833 回答
1

只要你在linux下,直接从文件系统中读取就可以找到很多答案:

File.open('/proc/mounts').each do |line|
  device, mount_point, file_system_type, mount_options, dump, fsck_order  = line.split(" ")
end

这导致您的问题的以下解决方案:

if File.readlines('/proc/mounts').any?{ |line| line.split(" ")[1] == "/mount_X"}
    puts "Yes, it is mounted!!!"
end
于 2015-11-06T15:34:12.903 回答
0

您可以只解析mount命令的输出:

`mount`.split("\n").grep(/bind/).map { |x| x.split(" ")[2]  }
于 2010-07-31T04:40:29.197 回答
0

@tvw 的回答对我来说有点扭曲。逐行读取 /proc/mounts 并在挂载点完整路径上进行部分字符串匹配mountpoint/folder_name

 raise "Failed:  not mounted" 
 unless File.readlines('/proc/mounts').any?{ |line| line.split(" ")[1] =~ /folder_name$/ }
于 2016-02-03T04:44:10.440 回答