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?

Eddie
  • 53,828
  • 22
  • 125
  • 145
mordechai
  • 127
  • 2
  • 10

2 Answers2

5

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 Z
  • 128,184
  • 27
  • 255
  • 279
  • 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 Apr 02 '09 at 16:05
  • so how do you do it with su db2inst1? – mordechai Apr 02 '09 at 17:54
  • oops, misread the question - I thought it was asking how to su to root to run the command. I'll fix that. – David Z Apr 02 '09 at 20:00
  • I am getting **sh: db2: not found**. It is the same like you hit db2 as root user. Does this command really run? – Tahir Feb 19 '13 at 13:03
0

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

nxadm
  • 2,079
  • 12
  • 17