2
当前我正在编写一个shell脚本,用于在数据库上执行批处理作业,并且希望在执行psql时将shell脚本操作写入日志文件或在某些情况下执行回滚。 像这样如何在shellcript中执行psql时添加shell命令
ConnectDb() {
\t PGPASSWORD=postgres psql -U postgres database -t -A -F , -v ON_ERROR_STOP=1 -v AUTOCOMMIT=0
}
printMsg() {
\t 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
有什么办法找回吗?
UPDATE 使用coprocess应该完美地工作。 对于遇到同样问题的人 UNIX Co process
coprocess似乎是我的最佳选择,但是当我更改连接到coproc时PGPASSWORD = postgres psql -U postgres数据库-t -A -F,-v ON_ERROR_STOP = 1 -v AUTOCOMMIT = 0 -q。当我运行终端时,终端表示coproc未找到,但如果直接在终端中运行,coproc工作。我应该在这里做什么设置? –
谢谢,原因是我的bash版本不支持coproc(v3.2),因为苹果不更新sh,所以使用zsh或ksh应该可以很好地工作。万分感谢。它让我安心一天 –