我有一个
nginx -V 2>&1 | \
grep -qi 'nginx/1.9.10\|ngx_pagespeed-release-1.9.32.10\|openssl-1.0.2f\|modsecurity-2.9.0' \
&& echo "has the stuff we need" \
|| echo "missing something"
这与
[root@mage2appblock vagrant]# nginx -V
nginx version: nginx/1.9.10
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.2f 28 Jan 2016
TLS SNI support enabled
configure arguments: --user=www-data --group=www-data
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx
--conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid
--lock-path=/var/lock/subsys/nginx
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
--add-module=/src/nginx/ngx_pagespeed-release-1.9.32.10-beta
--add-module=/src/nginx/modsecurity-2.9.0/nginx/modsecurity
--with-http_auth_request_module --with-http_sub_module
--with-http_mp4_module --with-http_flv_module
--with-http_addition_module --with-http_dav_module
--with-http_gunzip_module --with-http_gzip_static_module
--with-http_stub_status_module --with-http_sub_module
--with-http_v2_module --with-http_ssl_module
--with-openssl=/src/nginx/openssl-1.0.2f
--with-sha1=/usr/include/openssl
--with-md5=/usr/include/openssl --with-pcre --with-ipv6
--with-file-aio --with-http_realip_module
--without-http_scgi_module --without-http_uwsgi_module
似乎如果我改变子字符串
'nginx/1.9.10\|ngx_pagespeed-release-1.9.32.10\|openssl-1.0.2f\|modsecurity-2.9.0'
到
'nginx/1.9.10\|ngx_pagespeed-release-1.9.32.10\|openssl-1.0.2f\|modsecurity-2.9.1'
"has the stuff we need"
尽管并非所有内容都存在,但我仍然明白。我需要全部匹配或全部不匹配。
答案1
awk '/openssl-1.0.2f/ {test1=1} /nginx\/1.9.10/ {test2=1}
END { if (test1 && test2) print "has the stuff we need";
else print "missing something"}'
awk
如果需要,您还可以设置退出代码。
更新
较短版本(假设输入确实包含 0x1 个字符,则将带有换行符的输入视为单个“行”)
awk -v RS='\1' '/openssl-1\.0\.2f/ && /nginx\/1\.9\.10/ {
print "has the stuff we need"; exit};{print "missing something"; exit(1)}'
答案2
稍作修改即可工作:
[ $(nginx -V 2>&1 |
grep -cFf <(
echo 'nginx/1.9.10
ngx_pagespeed-release-1.9.32.10
openssl-1.0.2f
modsecurity-2.9.0'
)) -eq 4 ] &&
echo "has the stuff we need" ||
echo "missing something"
答案3
只需使用perl
并读取整个文件:
nginx -V 2>&1 | perl -0ne 'print "found\n" if m#nginx/1.9.10# &&
/ngx_pagespeed-release-1.9.32.10/ &&
/openssl-1.0.2f/ && /modsecurity-2.9.0/'
另请注意,问题文本中有一些隐藏字符。我不知道这些是否也存在于您的实际搜索字符串中,但是如果存在,它们会给您带来问题。如果我从你的问题中复制modsecurity-2.9.0
并传递它od -c
,我会得到:
$ echo modsecurity-2.9.0 | od -c
0000000 m o d s e c u r i t y - 2 . 9 .
0000020 342 200 214 342 200 213 0 \n
0000030
具体来说,根据uniprops
,您出现了 6 次U+FFFD <�> \N{替换字符}最后一个.
和最后一个之间0
。
答案4
has_all_iregexps() {
awk '
BEGIN {
if (ARGC <= 1) exit
for (i = 1; i < ARGC; i++) s[tolower(ARGV[i])]
n = ARGC - 1
ARGC = 1
}
{
for (i in s) if (tolower($0) ~ i) {
delete s[i]; if (!--n) exit
}
}
END {
if (n) {
print "Those regexps were not matched:"
for (i in s) print " " i
exit(1)
}
}' "$@" >&2
}
进而:
nginx -V 2>&1 |
has_all_iregexps 'nginx/1\.9\.10' \
'ngx_pagespeed-release-1\.9\.32\.10' \
'openssl-1\.0\.2f' \
'modsecurity-2\.9\.0' &&
echo "has the stuff we need"
或者:
has_all_istrings() {
awk '
BEGIN {
if (ARGC <= 1) exit
for (i = 1; i < ARGC; i++) s[tolower(ARGV[i])]
n = ARGC - 1
ARGC = 1
}
{
for (i in s) if (index(tolower($0), i)) {
delete s[i]; if (!--n) exit
}
}
END {
if (n) {
print "Those strings were not found:"
for (i in s) print " " i
exit(1)
}
}' "$@" >&2
}
nginx -V 2>&1 |
has_all_istrings 'nginx/1.9.10' \
'ngx_pagespeed-release-1.9.32.10' \
'openssl-1.0.2f' \
'modsecurity-2.9.0' &&
echo "has the stuff we need"
(tolower
s 用于不区分大小写的匹配,以匹配您对 的使用-i
,尽管我不确定您为什么要在这里进行不区分大小写的匹配)。