4

I'm trying to check if the installed nginx version is equal with the version defined in a config file.

My code:

#check version

command="nginx -v"
nginxv=$( ${command} 2>&1 )
nginxvcutted="echo ${nginxv:21}"
nginxonpc=$( ${nginxvcutted} 2>&1 )

if [ $nginxonpc != ${NGINX_VERSION} ]; then
  echo "${error} The installed Nginx Version $nginxonpc is DIFFERENT with the Nginx Version ${NGINX_VERSION} defined in the config!"
else
    echo "${ok} The Nginx Version $nginxonpc is equal with the Nginx Version ${NGINX_VERSION} defined in the config!"
fi

This code 'can' work, but I have got a problem: If the version number changes, the cut number (nginxv:21 in this example) doesn't fit anymore.

Example:

nginx-1.13.12 vs nginx-1.15.0 (13 vs 14 chars)

Is there any way to get this working, without that trouble?

Solution: I adapted the solution from @Mohammad Saleh Dehghanpour and it's working like a charm:

command="nginx -v"
nginxv=$( ${command} 2>&1 )
nginxlocal=$(echo $nginxv | grep -o '[0-9.]*$')

echo $nginxlocal
1.15.0
0

5 Answers 5

6

You can use regular expression instead of cut. For example to extract version number from nginx-1.15.0 use:

echo 'nginx-1.15.0' | grep -o '[0-9.]*$'

Output: 1.15.0

2
  • Thanks, your solution is working for me! I adapted it (storing the result into a variable). But if I'm trying to replace command="nginx -v" with command="uname -r" it doesn't work (the result is 64). But uname -r is showing 4.9.0-6-amd64 (the whole kernel release number). But I only need 4.9.0-6 (and 4.9.0-xxx for example). I already tried to manipulate the grep settings to [0-9.][0-9.][0-9.], but I'm not able to understand how it works actually.
    – Aeris
    Commented Jun 7, 2018 at 19:41
  • 1
    If someone has a long version string, then this approach will help you: echo "nginx version: nginx/1.14.0 (Ubuntu)" | awk -F' ' '{print $3}' | grep -o '[0-9.]*$'
    – Viaches
    Commented Apr 14, 2021 at 13:02
1

Combining everything on this page I got:

nginx -v 2>&1 | awk -F' ' '{print $3}' | cut -d / -f 2

0

You can use bash variable and then extract the versing using grep.

version=$(nginx -v 2>&1); echo $version | grep -o '[0-9.]*'
0

I'm surprised that the answers here are so complicated. There's no reason to involve awk or grep, which have more portability issues and marginally worse performance than plain old cut.

The actual problem is that nginx -v prints to STDERR instead of STDOUT, hence why the solutions that involve redirecting the error stream to the output stream (i.e. 2>&1) work.

The simplest solution:

nginx -v 2>&1 | cut -d'/' -f2

0
0

Some of the above comments don't account for nginx plus.

Something like:

nginx -v  2>&1 | grep -oP 'nginx/\K[0-9.]+'

Is better ( and something I use ).

On systems like alpine, that don't come with a version of grep that facilitates the above arguments. I use something like:

nginx -v 2>&1 | sed 's/.*nginx\///' |  cut -d ' ' -f1
➜ version=$(nginx -v 2>&1 | sed 's/.*nginx\///' |  cut -d ' ' -f1)
➜ echo $version                                                   
1.25.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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