Search code examples
unixdb2sql-scriptssu

How can I su from root to db2inst1 and invoke a SQL script, in one line?


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?


Solution

  • 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