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應該可以很好地工作。萬分感謝。它讓我安心一天 –