Db2 如何在AIX shell脚本中切换用户?

Db2 如何在AIX shell脚本中切换用户?,db2,aix,su,Db2,Aix,Su,我想从shell脚本运行DB2查询。我有一个执行DB2语句所需的用户db2inst1。我正在运行一个应用程序,它使用rootuser来运行shell脚本。这里,如果我将用户切换到db2inst1。我能做这项工作 下面是我的scipt,它与db2inst1一起完美运行 #!/bin/sh db2 "connect to customerdb" db2 "set schema = db2inst1" db2 "insert into tbl_customer(name,occupation) val

我想从shell脚本运行DB2查询。我有一个执行DB2语句所需的用户
db2inst1
。我正在运行一个应用程序,它使用
root
user来运行shell脚本。这里,如果我将用户切换到db2inst1。我能做这项工作

下面是我的scipt,它与
db2inst1
一起完美运行

#!/bin/sh
db2 "connect to customerdb"
db2 "set schema = db2inst1"
db2 "insert into tbl_customer(name,occupation) values ('Alex','Admin')"
将其视为root必须运行它。因此,root用户将允许脚本在执行db命令之前首先切换用户


我的应用程序将调用此shell脚本,该脚本将使用
root
user


如何在脚本中切换用户?我不熟悉shell脚本。

当您以
root用户身份运行时,您可以使用:

su dbinst1 -c './your_script arg1 arg2 ...'

如果您不是以
root
身份运行,生活将更加艰难。

如果您是以
root
身份运行,您可以使用:

su dbinst1 -c './your_script arg1 arg2 ...'

如果你不是以root用户身份运行,生活将更加艰难。

我的应用程序将调用这个使用root用户的shell脚本。我想把每件事都写成剧本。我尝试过:
sudb2inst1-c'db2使用admin连接到customerdb用户db2inst1;db2-c-i-w-td@-f/u01/IBM/workspace/addcustomer.sql'
但是我得到了
ksh:db2:not found.
环境可能没有正确设置;特别是,
$PATH
可以(重新)设置为默认值,该默认值不包括安装
db2
命令的位置。您可能还需要设置一些其他环境变量。这将在
yourscript
文件中完成——不管您如何称呼它。或者,它将被写入一个庞大的多行命令中,但运行脚本通常更整洁。在脚本中的每个语句中添加
su-dbinst1-c
。现在我的脚本是:
su-db2inst1-c'db2使用admin'su-db2inst1-c'db2-tvsf/u01/IBM/workspace/addcustomer.sql'连接到CustomerDB用户db2inst1
我不确定为什么需要成为root用户,然后使用
su
作为另一个用户执行脚本。这使得事情变得不必要的复杂。为什么不向正在执行脚本的ID授予在表中插入的访问权限(即运行应用程序的ID?我的应用程序将调用这个使用root用户的shell脚本。我希望所有内容都在脚本中。我尝试了:
su db2inst1-c'db2使用admin连接到customerdb用户db2inst1;db2-c-i-w-td@-f/u01/IBM/workspace/addcustomer.sql'
但是我得到了
ksh:db2:not found.
环境可能是bly未正确设置;特别是,
$PATH
可能是(re)设置为默认值,该值不包括安装
db2
命令的位置。您可能还需要设置一些其他环境变量。这将在
yourscript
文件中完成-无论您如何称呼它。或者,它将被写入一个大量的多行命令中,但运行脚本通常更整洁现在我的脚本是:
su-db2inst1-c'db2连接到customeradb用户db2inst1,使用admin'su-db2inst1-c'db2-tvsf/u01/IBM/workspace/addcustomer.sql'
作为另一个用途执行脚本r、 这使得事情变得不必要的复杂。为什么不将access INSERT授予正在执行脚本的ID(即运行应用程序的ID)到表中呢?