I have the following command which returns a multiline string and I want to put that into an array and iterate over it, which each line as a single element.
def get_mnt_pts
`grep VolGroup /proc/self/mounts | awk '{print $2, $4}' | awk -F, '{print $1}' | awk '{print $1, $2}'`
end
the data the command returns and is what the raw input to the array looks like before the split
/ rw
/home rw
/usr rw
/tmp rw
/var rw
/var/log rw
I have the following code which looks like it takes that data and splits it by newline and puts it into an array. I then call length on the array to confirm and I can pull out N value but when I do array.each it only ever grabs the first value.
I have a feeling I am dumping the whole string as a single element but then .length return back what I thought it should. I have checked through the Ruby doc's on arrays and also strings to see if I could figure it out. I am fairly certain I am creating the array wrong but not sure how.
mount_info = Array.new
mount_info = get_mnt_pts.split("\n")
puts mount_info.length #this returns 6
mount_info.each do |pt|
puts pt #only puts the first value of / rw
end
When I print out mount_info itself I get the following
print mount_info
["/ rw", "/home rw", "/usr rw", "/tmp rw", "/var rw", "/var/log rw"]
puts mount_info
/ rw
/home rw
/usr rw
/tmp rw
/var rw
/var/log rw
print mount_info[4] #returns back /var rw
puts mount_info[4] #returns back /var rw
Not sure where to look from here, any direction would be great. The end result is that for each element in the array I want to try to write a file in that directory to confirm the filesystem is in fact writable. The array contains a list of mount points and for each mount point I will write a Tempfile so the block below will take in the array and iterate through it.
def is_rw_test(mount_info)
mount_info.each do |pt|
Dir.exist? pt.split[0] || @crit_pt_test << "#{ pt.split[0] }"
file = Tempfile.new('.sensu', pt.split[0])
puts "The temp file we are writing to is: #{ file.path }" if config[:debug]
file.write("mops") || @crit_pt_test << "#{ pt.split[0] }"
file.read || @crit_pt_test << "#{ pt.split[0] }"
file.close
file.unlink
return @crit_pt_test
end
end
The code works fine but only for the first element in the array, it won't do it for each though, which is why I think my array assignment is borked.
Thanks
def is_rw_test(mount_info)
mount_info.each do |pt|
Dir.exist? pt.split[0] || @crit_pt_test << "#{ pt.split[0] }"
# Etc
return @crit_pt_test # ohai
end
end
You have a return
, so it returns, and no more iteration via each
.
If you want to collect values, then do something closer to:
def is_rw_test(mount_info)
mount_info.collect do |pt|
Dir.exist? pt.split[0] || @crit_pt_test << "#{ pt.split[0] }"
# Etc
@crit_pt_test
end
end
But if that's the case then an instance variable doesn't make any sense.
You need to decide what you actually want to do .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.