If you have multi servers Linux Distribution and should check only Ubuntu. Then you can use this script to select the os and if only match the OS then execute the command.
##!/bin/bash
if [ -e "/etc/issue" ] ;
then
issue=`cat /etc/issue`
set -- $issue
if [ $1 = "Ubuntu" ] ;
then
nginx -v 2>&1 | grep -o 1.* | cut -c1-6
fi
fi