8

Is there any or command to check whether NGINX is already installed on UBUNTU Linux using a bash command/script?

I was trying something like this

echo "BEGINNING INSTALLATION OF NGINX WEB SERVER"
echo
echo
echo "CHECKING TO SEE IF NGINX IS ALREADY INSTALLED"
service nginx > temp.install 2> temperr.install
echo 111
grep -c unrecognized temperr.install > temp2.install
echo 222
status = `cat temp2.install`
echo "NGINX STATUS $status" 

Am new to bash scripting and hence not sure if this is teh best possible way to approach this. I need to write a script that checks if NGINX is already installed or not. If it is not installed it simply installs NGINX otherwise it first deletes NGINX and then re-installs it.

  • 5
    How about dpkg -l | grep nginx – cnicutar Sep 18 '13 at 8:32
  • I tried this but it just returns back to the bash prompt without giving me any output. – Vikram Chakrabarty Sep 18 '13 at 8:34
  • 3
    @op Check return status with echo $? right after issuing the dpkg command. – cnicutar Sep 18 '13 at 8:35
  • 4
    Well... did not check return status but I just modified your command a bit and it works for me. This is what I did dpkg -l | grep -c nginx . If nginx is installed it returns the count of the nginx packages and if it is not installed it simply returns 0 – Vikram Chakrabarty Sep 18 '13 at 8:42
11
if ! which nginx > /dev/null 2>&1; then
    echo "Nginx not installed"
fi

or

if [ ! -x /usr/sbin/nginx ]; then
    echo "Nginx not installed"
fi

or if you want to be Debian/Ubuntu specific:

if ! dpkg -l nginx | egrep 'îi.*nginx' > /dev/null 2>&1; then
    echo "Nginx not installed"
fi

if you're into the whole brevity thing:

! test -x /usr/sbin/nginx && echo "Nginx not installed"
| improve this answer | |
  • Just the little caveat that this only finds if nginx is installed the standard way. not if it is compiled from source and installed in for instance /usr/local/bin – Lenne Jul 1 '16 at 20:11
  • The last answer is awesome :) – Faris Rayhan Sep 9 '17 at 20:23
1

try this:

command -v nginx

install if not installed:

command -v nginx || sudo apt install nginx
| improve this answer | |
  • command is not found in cron job, I am not able to find full path for command, ie using which command – Ramratan Gupta Jan 3 at 13:19
0

I build below solution, which run also in cron job

ISNGINX_INSTALLED=`/bin/ls /usr/sbin/nginx`
if [[ ! -z $ISNGINX_INSTALLED ]]; then
    echo "NOT installed"
else
    echo "installed"
fi
| improve this answer | |

Your Answer

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