检测进程替换时的退出状态
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检测进程替换时的退出状态相关的知识,希望对你有一定的参考价值。
我目前正在使用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循环中的内容
答案
跟踪流程替换的退出状态是棘手的,并且需要一个非常现代的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
}
以上是关于检测进程替换时的退出状态的主要内容,如果未能解决你的问题,请参考以下文章