【发布时间】:2017-08-06 00:09:25
【问题描述】:
目前我正在编写一个在数据库上执行批处理作业的 shell 脚本,我想在执行 psql 时将 shell 脚本操作(如写入日志文件或回滚)放在某些条件下。 像这样
ConnectDb() {
PGPASSWORD=postgres psql -U postgres database -t -A -F , -v ON_ERROR_STOP=1 -v AUTOCOMMIT=0
}
printMsg() {
echo "$PROGRAM/$SUBMODULE $(date "+ %Y%H%s")" $1 | tee -a ~/Desktop/shell/log/test.log
}
ConnectDb <<EOF
start transaction;
select * from ...;
# do some database stubs here
# i want to add somthing like this
printMsg "Querying ..."
# many shell script command go here
if [ some accepted condition ] commit;
if [ some bad conditions ] rollback;
if [ should do more database query ] do insert, update, delete to database
commit;
EOF
有什么方法可以找回吗?
更新 使用 coprocess 应该可以正常工作。 对于那些遇到同样问题的人 UNIX Co process
【问题讨论】: