views:

293

answers:

2

How can I su from root to db2inst1 and invoke a SQL script all in 1 line? I am thinking about something like this:

su db2inst1 | db2 CONNECT TO myDatabase USER db2inst1 USING mypw; db2 -c -i -w -td@ -f /tmp/deploy/sql/My.sql | exit;

Any ideas?

+4  A: 

You can use the -c or --command=<command> option to execute a command with su. In your case, something like this:

su -c 'db2 CONNECT TO myDatabase USER db2inst1 USING mypw; db2 -c -i -w -td@ -f /tmp/deploy/sql/My.sql' db2inst1
David Zaslavsky
Good answer, but you forgot to su to user db2inst1, which is not the same thing as using that user in the db2 command. You might want to edit that in...
dwc
so how do you do it with su db2inst1?
oops, misread the question - I thought it was asking how to su to root to run the command. I'll fix that.
David Zaslavsky
A: 

if using Solaris and you don't want/can enter password, checkout pfexec.