![](/img/trans.png)
[英]How to exit the entire call stack of shell scripts if a child script fails?
[英]Exit shell script if psql throws error in exectuting sql scripts
我有以下 shell 腳本
#! /bin/sh
for file in $(ls -l scripts/*.sql | awk '{print $NF}'); do
psql -h host -d db -U user -p port -f $file;
result=$?
if [ $result -ne 0 ]
then
exit 1
fi
done
即使我在 sql 文件中添加了一些錯誤,$result 總是為 0。 如果任何 sql 腳本拋出錯誤,我想退出 shell 腳本。 我在捕捉最后執行語句的錯誤時是否遺漏了什么
試試: psql -v ON_ERROR_STOP=1 -f $file
這使得 psql 首先停止 SQL 錯誤並在這種情況下返回非零返回碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.