检查fstab订单

时间:2016-02-14 18:43:34

标签: python bash

我正在尝试编写一个python脚本来检查" parent"装点 存在于" child"例如,挂载点 /mnt应该在/mnt/mount1之前

这是我到目前为止所写的内容

#!/usr/bin/python

for line in open("/etc/fstab", "r"): 
  line = line.strip() 
  if line == "":
     continue

  lintyp = linetype(line)  
  if lintyp >= type:
     type = lintyp

使用python甚至bash

1 个答案:

答案 0 :(得分:0)

您可以使用fstab模块,我使用StringIO.StringIO替换open('/etc/fstab', 'r')

import StringIO
from fstab import Fstab

s = content = """\
# this is a comment, followed by an empty line
# the next line is a syntax error
yo!
#UUID="2b0ab63e-fd1b-4d7a-b021-3827b4aa4c8b" /media/hda3 ext3 defaults 0 2
/dev/hda2 /boot ext3 defaults 0 2
/dev/hda1 / ext3 defaults,errors=remount-ro 0 1
"""

fstab = Fstab()
#fstab.read(open('/etc/fstab', 'r'))
fstab.read(StringIO.StringIO(content))

output = []
_fs = []
for line in fstab.lines:
    if line.has_filesystem():
        _fs.append(line.raw)
    else:
        output.append(line.raw)

_fs = sorted(_fs)
output.extend(_fs)


output = '\n'.join(line for line in output)
print output
#You have just to write to '/etc/fstab'