我有以下配置文件:
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
#auth required pam_wheel.so use_uid
auth substack system-auth
auth include postlogin
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
account include system-auth
password include system-auth
session include system-auth
session include postlogin
session optional pam_xauth.so
空格似乎是标签。打开vim中的列表(:set list)显示:
#%PAM-1.0$
auth^I^Isufficient^Ipam_rootok.so$
# Uncomment the following line to implicitly trust users in the "wheel" group.$
#auth^I^Isufficient^Ipam_wheel.so trust use_uid$
# Uncomment the following line to require a user to be in the "wheel" group.$
#auth^I^Irequired^Ipam_wheel.so use_uid$
auth^I^Isubstack^Isystem-auth$
auth^I^Iinclude^I^Ipostlogin$
account^I^Isufficient^Ipam_succeed_if.so uid = 0 use_uid quiet$
account^I^Iinclude^I^Isystem-auth$
password^Iinclude^I^Isystem-auth$
session^I^Iinclude^I^Isystem-auth$
session^I^Iinclude^I^Ipostlogin$
session^I^Ioptional^Ipam_xauth.so$
我想在线上匹配:
#auth required pam_wheel.so use_uid
整条线上的匹配不起作用。我不确定为什么......猜测它与空格标签有关:
grep "#auth required pam_wheel.so use_uid" /etc/pam.d/su
(返回不匹配)
所以,我认为通过说:
来尝试匹配是值得的grep "#auth\s+required\s+pam_wheel.so\s+use_uid" /etc/pam.d/su
我正在读这个正则表达式为“#auth”,后跟至少一个或多个制表符/空格,后跟“required”,后跟至少一个或多个制表符/空格,后跟“pam_wheel.so”等。
但是,这也不匹配。我不确定这里出了什么问题。我错过了什么?
答案 0 :(得分:0)
您可以在正则表达式中使用与空格或制表符匹配的[[:blank:]]
:
grep '#auth[[:blank:]]*required[[:blank:]]*pam_wheel\.so[[:blank:]]*use_uid' /etc/pam.d/su
#auth required pam_wheel.so use_uid
答案 1 :(得分:0)
默认情况下,grep使用基本正则表达式(BRE)引擎。如果您希望扩展正则表达式引擎(ERE)支持Chan
等字符类快捷方式,则需要运行\s
或egrep
。例如:
grep -E
或者,您可以使用内置的Perl兼容正则表达式(PCRE)库使用$ egrep "#auth\s+required\s+pam_wheel\.so\s+use_uid" /etc/pam.d/su
#auth required pam_wheel.so use_uid
或grep -P
来使用grep。但是,并非所有平台都支持此功能。
pcregrep
了解有关BRE和ERE之间差异的详细信息。man 7 re_format
有关PCRE库和语法的信息。答案 2 :(得分:0)
感谢。我没有意识到我需要使用扩展表达式,并没有逃避。