Ruby check if something is mounted(--bind) in this directory
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)
How to check with ruby(not shell!!开发者_JAVA百科) whether some dir is mounted on /mount_X?
Is there something easier than opening /proc/mounts and looking for /mount_X there?
Another way to do it is:
system("mount|grep /mount_X")
As long as you are under linux, you find many answers directly by reading from the filesystem:
File.open('/proc/mounts').each do |line|
device, mount_point, file_system_type, mount_options, dump, fsck_order = line.split(" ")
end
which leads to the following solution for your problem:
if File.readlines('/proc/mounts').any?{ |line| line.split(" ")[1] == "/mount_X"}
puts "Yes, it is mounted!!!"
end
You can just parse the output of the mount
command:
`mount`.split("\n").grep(/bind/).map { |x| x.split(" ")[2] }
a little twist to @tvw's answer did it for me. Read /proc/mounts line by line and do a partial string match on the
mountpoint full path mountpoint/folder_name
.
raise "Failed: not mounted"
unless File.readlines('/proc/mounts').any?{ |line| line.split(" ")[1] =~ /folder_name$/ }
精彩评论