Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Sep 20 '13 at 11:11

This question came from our site for professional and enthusiast programmers.

3  
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
2  
@op Check return status with echo $? right after issuing the dpkg command. –  cnicutar Sep 18 '13 at 8:35
1  
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

1 Answer 1

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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.