正则表达式为Linux文件权限(数字符号) [英] Regex for Linux file permissions (numeric notation)

查看:252
本文介绍了正则表达式为Linux文件权限(数字符号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能为我的生活弄清楚正确的正则表达式这一点。

I can't for the life of me figure out the proper regex for this.

我要找的是一个正则表达式匹配一个有效的数值再$ P $的Linux文件权限psentation(例如740全读无,777为全全全部)。到目前为止,我已经试过如下:

What I'm looking for is a regex to match a valid numeric representation of Linux file permissions (e.g. 740 for all-read-none, 777 for all-all-all). So far I've tried the following:

strtotest=740
echo "$strtotest" | grep -q "[(0|1|2|3|4|5|7){3}]"
if [ $? -eq 0 ]; then
    echo "found it"
fi

与上面的问题是,正则表达式匹配任何与 1-5 7 中,而不管的任何其他字符。例如,如果 strtotest 分别更改为 709 ,有条件的将是真实的。我也试过 [0 | 1 | 2 | 3 | 4 | 5 | 7 {3}] [(0 | 1 | 2 | 3 | 4 | 5 | 7 {3})] ,但那些不工作,以及

The problem with the above is that the regex matches anything with 1-5 or 7 in it, regardless of any other characters. For example, if strtotest were to be changed to 709, the conditional would be true. I've also tried [0|1|2|3|4|5|7{3}] and [(0|1|2|3|4|5|7{3})] but those don't work as well.

是我用错了,还是我失去的东西,有对付的grep

Is the regex I'm using wrong, or am I missing something that has to deal with grep?

推荐答案

这是会为你工作最简单,最明显的正则表达式是:

The simplest and most obvious regexp which is going to work for you is:

grep -q '(0|1|2|3|4|5|7)(0|1|2|3|4|5|7)(0|1|2|3|4|5|7)'

下面是一个优化版本:

grep -Eq '(0|1|2|3|4|5|7){3}'

自6可以重新present权限,以及,我们可以进一步优化它:

since 6 can represent permissions as well, we could optimize it further:

grep -Eq '[0-7]{3}'

这篇关于正则表达式为Linux文件权限(数字符号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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