Solved

shell script

Posted on 2014-02-14
5
382 Views
Last Modified: 2014-03-15
sudo -u postgres -H sh -c "cd /usr/local/src/postgresql-8.2.10;export PERL5LIB=/usr/lib/perl5;export CC="gcc -m32";export LD="ld -melf_i386";export CFLAGS="-m32";export LDFLAGS="-m32";setarch i386"

I am trying to write script to execute the above lines.  How can I script it?  

1. I need to login as postgres account
2. need to set the following variables

export PERL5LIB=/usr/lib/perl5  
export CC="gcc -m32"
export LD="ld -melf_i386"
export CFLAGS="-m32"
export LDFLAGS="-m32"
setarch i386

3.  need to run the following command
./configure --prefix=/pgarc/pgsql --with-perl

4. gmake
0
Comment
Question by:ittechlab
5 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 250 total points
ID: 39860454
Put the commands in a file (vg. MyScrtpt.sh)1

#!/bin/ksh
#
# Script: MyScrtpt.sh
#
# 1. I need to login as postgres account

cd /usr/local/src/postgresql-8.2.10

sudo  su -  postgres    #<= Here goes the postgres account ...

# 2. need to set the following variables

export PERL5LIB=/usr/lib/perl5  
export CC="gcc -m32"
export LD="ld -melf_i386"
export CFLAGS="-m32"
export LDFLAGS="-m32"
setarch i386

# 3.  need to run the following command

./configure --prefix=/pgarc/pgsql --with-perl

# 4. gmake 

gmake          #<= What goes here ????

exit

Open in new window

Then make it executable:
chmod +x MyScrtpt.sh

Open in new window

0
 

Author Comment

by:ittechlab
ID: 39860484
[root@rhel5 scripts]# ./myscript.sh
[postgres@rhel5 ~]$

when I run this script its su - postgres and waits there.

[postgres@rhel5 ~]$ exit
logout
-sh-3.2#

-sh-3.2#exit

then it is started to run the ./configure part...

why this happens.
0
 

Author Comment

by:ittechlab
ID: 39860486
my concern is it should run as soon as su - postgres.
0
 
LVL 5

Expert Comment

by:Dave Gould
ID: 39861396
The sudo should be outside the script and the script run by the sudo command.

ie comment out the sudo line in your script and then run the script using

sudo -u postgres myscript.sh
0
 
LVL 26

Assisted Solution

by:skullnobrains
skullnobrains earned 250 total points
ID: 39886569
try this instead of your su line

if test `whoami` != postgres
then sudo su postgres -c "/bin/sh $0"
  exit $?


or alternatively

cat <<EOF |sudo su postgres -c "/bin/sh"
....
EOF
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now