Script for removing Postgres


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for removing Postgres
# 1  
Old 09-23-2013
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.
Code:
#!/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

Moderator's Comments:
Mod Comment Please use code tags next time for your code or data

Last edited by vbe; 09-23-2013 at 05:39 AM.. Reason: code tags
This User Gave Thanks to dannyvdberg For This Post:
# 2  
Old 09-26-2013
I like it, simple and gets the job done Smilie
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.
# 3  
Old 09-26-2013
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...
Code:
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} }

Mine's (tui-bol-yesno) actualy a file with other code before.... (relevant are: read and case statements):
Code:
    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:
Code:
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!

Last edited by sea; 09-26-2013 at 11:03 PM..
This User Gave Thanks to sea For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using shell scripting for making queries on postgres sql

I have a situation where I have a list of airplanes that make a series of flights. Therefore I am looking up the different flights that each airplane makes based on a postgres sql query: select flightid from plane where airplane='DELTAx' As a result I get a series of flight numbers... (0 Replies)
Discussion started by: JSNY
0 Replies

2. Shell Programming and Scripting

Taking information from a postgres sql query and putting it into a shell script array

I have a postgres sql statement that is the following: select age from students; which gives me the entries: Age --- 10 15 13 12 9 14 10 which is about 7 rows of data. Now what I would like to do with this is use a shell script to create an array age. As a results... (3 Replies)
Discussion started by: JSNY
3 Replies

3. UNIX for Advanced & Expert Users

How to set postgres process in monit file?

Dear Friends, I need to add the postgres process in monit file ( Debian machine ). How to add that process in monit?. If anyone know the solution for this, pls let me know. ---------- Post updated at 02:27 PM ---------- Previous update was at 12:21 PM ---------- I have found the... (0 Replies)
Discussion started by: rekha_sri
0 Replies

4. Shell Programming and Scripting

Postgres in Linux

I have this 15 postgres sql queries similar to below to run in linux... Its taking a lot of time to run (3hours) . can any one plz guide me how can i reduce the time of execution execute 'insert into cc.rpt_cons_sub_ccdb_data(report_date, server_name, report_type, count) select... (3 Replies)
Discussion started by: nikhil jain
3 Replies

5. Shell Programming and Scripting

help copy *.txt to postgres

hi all, a have problem to load *.txt to postgres my database: id_list_ip (nextval) list_ip (varchar) txt file (list_ip.txt) hasilping_10.8.248.1 hasilping_119.110.112.226 hasilping_119.110.125.33 hasilping_125.22.1.25 hasilping_192.168.2.1 hasilping_202.73.96.70 script... (3 Replies)
Discussion started by: adi0926
3 Replies

6. Shell Programming and Scripting

testing postgres connection in shell script

I have a script that does db query in postgres. I'm trying to put in some error checking in the script. I keep running into the password prompt problem where when invalid credentials/or database is put it, it would prompt me for a password which hangs the script. Is there a way that I can check for... (0 Replies)
Discussion started by: zerofire123
0 Replies

7. Shell Programming and Scripting

Backup-Script for Postgres

Hi, I'm working with postgres. Now I found a script to backup my databases. # #!/bin/bash # # # setup environment PG_PORT=5432 PG_HOME=/usr/lib/postgresql/8.3 PG_VAR=/backup PG_DATA=/backup/data PG_BACKUPS=/backup/backup PG_LOGS=/backup/logs ... (6 Replies)
Discussion started by: karl_ha
6 Replies

8. Shell Programming and Scripting

Postgres : pg_dump Error

hi friends, i have script to take backup of postgresql server ....i am getting error like access denied myscript.sh sendEmail=/home/backup/scripts/sendEmail.pl DAY=`/bin/date +%d-%m-%y` DIR=/home/backup/psql/$DAY test -d $DIR | mkdir -p $DIR dblist="test test1 postgres" for db in... (8 Replies)
Discussion started by: jagnikam
8 Replies

9. Shell Programming and Scripting

how will i connect postgres database from unix shell script

I have got the solution so just closing this issue. (3 Replies)
Discussion started by: jambesh
3 Replies
Login or Register to Ask a Question