0

I have a

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"

which is going against

[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

Seems that if I change the substrings from

'nginx/1.9.10\|ngx_pagespeed-release-1.9.32.10\|openssl-1.0.2f\|modsecurity-2.9.‌​0'

to

'nginx/1.9.10\|ngx_pagespeed-release-1.9.32.10\|openssl-1.0.2f\|modsecurity-2.9.‌​1'

I still get "has the stuff we need" even though not everything was present. I need to match all or nothing.

12
  • 3
    | in grep-speak is "or", not "and"
    – Jeff Schaller
    Feb 2, 2016 at 18:48
  • try something along the lines of: nginx -V | grep string1 | grep string2 | grep string3... | grep -q last-string; if $? is 0, you found them all, otherwise you're missing one
    – Jeff Schaller
    Feb 2, 2016 at 18:50
  • 1
    @JeffSchaller yes I see that now. ok, just need to re do for and and version, ok..
    – Quantum
    Feb 2, 2016 at 18:50
  • @JeffSchaller not 100% I'm following, but out=$(nginx -V 2>&1); echo $out | grep -qi "nginx/1.9.10" | grep -qi "ngx_pagespeed-release-1.9.32.10" | grep -qi "openssl-1.0.2f" | grep -qi "modsecurity-2.9.0" && echo "has the stuff we need" || echo "missing something" is always hitting "missing something" when it is true..
    – Quantum
    Feb 2, 2016 at 19:27
  • 1
    Then leave the -q on the last grep....
    – Jeff Schaller
    Feb 2, 2016 at 19:45

4 Answers 4

2
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"}'

You can also set the exit code of awk if you need that.

update

shorter version (treats input with line breaks as a single "line" assuming the input does contain 0x1 characters)

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
  • this seems like it would be a lot longer of a one liner then echo $(nginx -V 2>&1) | grep -i "nginx/1.9.10" | grep -i "ngx_pagespeed-release-1.9.32.10" | grep -i "openssl-1.0.2f" | grep -qi "modsecurity-2.9.0" && echo "has the stuff we need" || echo "missing something" is there a way to shorten yours?
    – Quantum
    Feb 2, 2016 at 19:47
  • @jeremy.bass Yes, can be done easier. Feb 2, 2016 at 19:59
1

Work with a little modification:

[ $(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"
1

Just use perl and slurp the file whole:

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/'

Also, note that you have some hidden characters in the text of your question. I don't know if those are also there in your actual search string but, if they are, they will cause you problems. If I copy the modsecurity-2.9.‌​0 from your question and pass it through od -c, I get:

$ 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

Specifically, according to uniprops, you have 6 occurrences of U+FFFD ‹�› \N{REPLACEMENT CHARACTER} between the last . and the 0.

0
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
}

And then:

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"

Or:

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"

(the tolowers are for case insensitive matching to match your use of -i, though I'm not sure why you'd want to do case insensitive matching here).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.