Script for removing Postgres

I made a script to remove Postgres if this is already installed on your system. I have a other script to install Postgres, so this script can be used before you going to install Postgres.

Do you like this script? I would love to hear feedback.

#!/bin/bash
#
#################################
#
# Script to remove Postgres
#
#################################

#function
prompt_ynq () {
   echo -e "\e[01;31m${1}\e[00m"
   PS3="Maak je keuze ('s' voor stop): "
   INSTALL=false
   select ANSWER in "ja" "nee"; do
        case ${ANSWER} in
         *    ) INSTALL=${ANSWER};  break;;
      esac
   done
   check_stop ${INSTALL}
}

#function
check_stop () {
   if [ -z ${1} ]
   then
      echo -e "\n\e[01;31mYou've chosed to quit.\e[00m\n"
      exit
   fi
}

echo -e "\e[01;31mSearching for Postgres\e[00m"
sleep 3
echo ""

if [ -f postgres* ] && [ -f postgresql* ]
        then
        echo -e "\e[01;31mFound versions of Postgres:\e[00m"
        dpkg -l | grep postgres
        echo ""
        prompt_ynq "Do you want to remove this versions of Postgres?"

        if [ ${ANSWER} = "ja" ]
                then
                sudo apt-get --purge remove postgresql-* postgresql-client-* postgresql-client-common-* postgresql-common-* postgresql-contrib-*
                sudo rm -rf /var/lib/postgresql/
                sudo rm -rf /var/log/postgresql/
                sudo rm -rf /etc/postgresql/

        else
        echo ""
        echo -e "\e[01;31mYou've chosen to remove Postgres\e[00m"
        echo ""
        fi

        else
        echo "No Postgres found"
fi 

I like it, simple and gets the job done :slight_smile:
Might I make a suggestion though - You really should check the output of the sudo apt-get command - if that fails, you'd probably not want to start rm'img files. Even just appending || exit 1 to teh line would give you a saftey net.

Also, to make it compatible with more linux distribuitions, consider checking for yum vs apt and adjust the commandline accordingly.

I'd rather make it english output first than add a handler for yum/apt-get...

Either way:
Very nice approach, for future, try to make each function just to that single thing it should.
Make other functions depend on it, rather than call them from another function.

Example.. yours...

prompt_ynq () {
   echo -e "\e[01;31m${1}\e[00m"
   PS3="Maak je keuze ('s' voor stop): "
   INSTALL=false
   select ANSWER in "ja" "nee"; do
         case ${ANSWER} in
         *    )         INSTALL=${ANSWER};  break;;
         esac
   done
   check_stop ${INSTALL} }

[LEFT] Mine's (tui-bol-yesno) actualy a file with other code before.... (relevant are: read and case statements):
[/LEFT]

    read -p "$BORDER_LEFT $1 (y/n)" -n1 answer 
    printf "\r"
    tui-echo "$1 (y/n)" "$answer"
    case $answer in
        y|o|j|s|Y|O|J|S)
            # First letter of the meaning "yes" in these languages:
            # English, Fran�ais, Deutsch, Italiano
            exit 0    ;;
        *)  exit 1    ;;
    esac

As you see, i do an exit code, wich enables me to do use it instead of CONDITIONS....
EG:

if tui-bol-yesno "Question to answer"
then echo "yep"
else echo "nope"
fi

Working with exit codes (error levels) takes a time to get used to, but is hell of alot easier to work with in the long run!