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 更容易的吗?
另一种方法是:
system("mount|grep /mount_X")
只要你在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
您可以只解析mount
命令的输出:
`mount`.split("\n").grep(/bind/).map { |x| x.split(" ")[2] }
@tvw 的回答对我来说有点扭曲。逐行读取 /proc/mounts 并在挂载点完整路径上进行部分字符串匹配mountpoint/folder_name
。
raise "Failed: not mounted"
unless File.readlines('/proc/mounts').any?{ |line| line.split(" ")[1] =~ /folder_name$/ }