我当前正在使用bash 4.1,并且正在使用一个函数在存储库文件上执行SVN cat。之后,在每行上迭代以执行一些转换(主要是串联等)。如果上述文件不存在,脚本应停止并显示一条错误消息。脚本如下:
function getFile {
svnCat=`svn cat (file) 2>&1`
if [[ -n $(echo "$svnCat" | grep "W160013") ]]; then # W160013 is the returned value by SVN stderr case a file doesn't exist
echo "File doesn't exist" >&2
exit 1
else
echo "$svnCat" | while read -r; do
#Do your job
done
fi
}
function processFile{
while read -r do
#do stuff
done < <(getFile)
#do even more stuff
}
但是,在文件不存在的情况下,错误消息只打印一次,但脚本会继续执行。有没有一种方法可以检测到while循环失败并且应该完全停止脚本?
不能使用set -e选项,因为我需要删除在此过程中创建的一些文件。
更新:我尝试添加||。在完成命令后退出,如下所示:
function processFile{
while read -r do
#do stuff
done || exit 1 < <(getFile)
但是,该脚本正在等待用户输出,当我按Enter键时,它将在while循环中执行内容
答案 0 :(得分:0)
跟踪流程替换的退出状态非常棘手,并且需要非常现代的bash版本(我想说的是4.3或更高版本)。在此之前,$!
之后的<(getFile)
将无法正确填充,因此wait
将失败(或更糟糕的是,请参考先前启动的子流程)。
#!/usr/bin/env bash
### If you *don't* want any transforms at this stage, eliminate getFile entirely
### ...and just use < <(svn cat "$1") in processFile; you can/should rely on svn cat itself
### ...to have a nonzero exit status in the event of *any* failure; if it fails to do so,
### ...file a bug upstream.
getFile() {
local content
content=$(svn cat "$1") || exit # pass through exit status of failed svn cat
while read -r line; do
echo "Generating a transformed version of $line"
done <<<"$content"
}
processFile() {
local getFileFd getFilePid line
# start a new process running getFile; record its pid to use to check exit status later
exec {getFileFd}< <(getFile "$1"); getFilePid=$!
# actual loop over received content
while IFS= read -r line; do
echo "Retrieved line $line from process $getFilePid"
done <&"$getFileFd"
# close the FIFO from the subprocess
exec {getFileFd}<&-
# then use wait to wait for it to exit and collect its exit status
if wait "$getFilePid"; then
echo "OK: getFile reports success" >&2
else
getFileRetval=$?
echo "ERROR: getFile returned exit status $getFileRetval" >&2
fi
}