正则表达式匹配子挂载点 [英] regex to match child mount points

查看:320
本文介绍了正则表达式匹配子挂载点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写bash脚本,用于检查小孩坐骑不退出之前fstab文件父母坐骑

I am writing bash script for checking that "child mounts" doesn't exits before "parent mounts" on fstab

即:如果我有fstab中的坐骑如下:

i.e: if I have the following mounts on fstab:

好:父母首先挂载点

/Application
/Application/home

坏:孩子第一挂载点

bad: child mount point first

/Application/home
/Application/

所以我想循环中的所有挂载点fstab中,比赛比作正则表达式,将检查的挂载点+ /捕获所有子安装点正则表达式。在这种情况下,我想赶上那有/应用/挂载点的挂载点。

So I want to loop all the mount points in fstab and compare the match to a regex that will check regex of "mount point" +/ to catch all child mount points. In this case I want to catch any mount point that have /application/ mount point.

我想

for child in $MATCH;do
  if [[ "$child" =~ [/\/application\//] ]];then
    echo "$child has child mounts before it"
  fi
done

但它捕获更多的比赛超过预期。请帮我用正确的正则表达式。

but it catches more matches than expected. Please help me with the correct regex.

推荐答案

由于从/ etc / fstab中,由新线分离,像这样的,例如所有的挂载点的文件:

Given a file with all the mount points from /etc/fstab, separated by a new-line, like this one for example:

/Application/home
/Application/

您可以执行以下

的grep -n'/应用/'mount_points_file |排序-k2 -t \\:

将输出:

2:/Application/
1:/Application/home

在此步骤中,您可以看到的 /应用/ 的出现在2号线,而其子的 /应用/家居的出现在1号线,其中定义为一个坏的订单。

From this step you can see that /Application/ appears on line number 2, while its child /Application/home appears on line number 1, which you define as a bad order.

实际上排序 -c 选项可以检测疾病并告知你有关它的标准错误的:

Actually sort's -c option can detect a disorder and inform you about it in stderr:

的grep -n'/应用/'mount_points_file |排序-t \\:-k2 |排序-c -t \\:-k1n

将输出:

排序 - :2:障碍:1:/应用/家居

但我认为它会停在第一个无序的条目。

But I think it will stop on the first disordered entry.

另外:我不认为 / etc / fstab文件的顺序问题。
我运行的Archlinux与我有的/ boot 的之前的 / 的。

Aside: I don't think /etc/fstab's order matter. I run Archlinux with an fstab in which I have /boot before /.

这篇关于正则表达式匹配子挂载点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆