You are not logged in.

#1 2010-12-29 01:20:36

zayac
Member
From: Dolgoprudny
Registered: 2009-03-20
Posts: 25

Bash scripting question

Hello!
I need to create a script which downloads and configures PostgreSQL. The  idea is rather simple - to put in the script all commands that are on PostgreSQL ArchWiki page.The first problem I have is related to creating new user.
This script doesn't work correctly.

#!/bin/bash
echo 'Downloading postgresql...'
pacman -S postgresql --needed --noconfirm --quiet
echo 'Running daemon'
/etc/rc.d/postgresql restart
su - postgres
echo 'Creating new user...'
createuser -RSP newuser

What can you recommend to make this code work properly from the script?

Offline

#2 2010-12-29 02:37:40

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: Bash scripting question

Where in the script is it failing?  what error message, if any, is being displayed?
that should give you some clues where the script is failing.

also, it would make your script work much more elegantly and safely if you were to take a look at using program exit statuses to determine if a program has executed as expected, and if not, exit the script smoothly.

here is an example:

pacman -S postgresql --needed --noconfirm --quiet
if [ $? -eq 0 ] ; then
         echo "Download and installation successful, starting daemon..."
         . . .
         . . .
else
         echo "aw crap, pacman failed! you should probably look into why!!!"
fi

 
there are a lot of fun things that you can do with the if else statements and exit statuses. You can do something similar with checking UID's to determine if the script is being run as root, or if the su to the postgresql user is hapening properly.

I'll leave the rest to you as a learning excercise big_smile

I am kind of curious as to your motivation to write a script for this also.

Last edited by Cyrusm (2010-12-29 03:03:09)


Hofstadter's Law:
           It always takes longer than you expect, even when you take into account Hofstadter's Law.

Offline

#3 2011-01-01 01:47:53

zayac
Member
From: Dolgoprudny
Registered: 2009-03-20
Posts: 25

Re: Bash scripting question

Ok. Thanks for good tips. I have one more problem.
So when I run the script mentioned above I receive output like

......
Starting postgresql             [DONE]
[postgres@host ~]$

So it seems that that "su - postgres" was successful, as I am in a terminal of postgres user. However the last command "createuser -RSP newuser" wasn't executed, as -P option is for password prompt, but there wasn't any prompt issued during script execution process. I receive this question when I run "exit" in the terminal. The first solution of the problem is to provide password in the script, but it is better if the password would be unique for each script user. How can I do this?

I need this just to provide a clear installation guide for my server application users.

Offline

#4 2011-01-01 01:54:23

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bash scripting question

su - postgres
echo 'Creating new user...'
createuser -RSP newuser

The problem is that you're expecting your script to continue processing after invocation of the 'su' command... which doesn't happen because su creates a new process which blocks continuation of your script. the createuser command will execute after the shell created for the postgres user terminates.

Offline

Board footer

Powered by FluxBB