-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpipefail.functions
70 lines (62 loc) · 2.34 KB
/
pipefail.functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# This file is part of shellfire core. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT. No part of shellfire core, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire core. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT.
# Original Copyright (c) 2003-2005, Joe Halpin <j.p.h@comcast.net>
# Modified from http://cfajohnson.com/shell/cus-faq-2.html#Q11
# Original permissively licensed
# Modifications Copyright © 2014-2015 The developers of shellfire core. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT.
# Uses the file descriptors 3 and 4
# Run as com1 \| com2 \| com3
core_pipefail_execute()
{
_core_pipefail_execute_unsetPipestatusVariablesFromPreviousRun
local index=1
local pipelineCommand=''
local k=1
local l=''
local a=''
for a in "$@"
do
if [ "$a" = '|' ]; then
pipelineCommand="$pipelineCommand { $l "'3>&-
echo "core_pipefail_PIPESTATUS_'$index'=$?" >&3
} 4>&- |'
index=$((index + 1))
l=''
else
l="$l \"\$$k\""
fi
k=$((k + 1))
done
pipelineCommand="$pipelineCommand $l"' 3>&- >&4 4>&-
echo "core_pipefail_PIPESTATUS_'$index'=$?"'
core_message DEBUG "About to execute pipeline command: '$pipelineCommand'"
exec 4>&1
eval "$(exec 3>&1; eval "$pipelineCommand")"
exec 4>&-
local exitCode
_core_pipefail_execute_setPipestatusVariablesAndExitCode
return $exitCode
}
_core_pipefail_execute_unsetPipestatusVariablesFromPreviousRun()
{
local index=1
while core_variable_isSet core_pipefail_PIPESTATUS_$index
do
core_variable_unset core_pipefail_PIPESTATUS_$index
index=$((index + 1))
done
}
_core_pipefail_execute_setPipestatusVariablesAndExitCode()
{
local index=1
local core_variable_indirectValue_result
while core_variable_isSet core_pipefail_PIPESTATUS_$index
do
core_variable_indirectValue core_pipefail_PIPESTATUS_$index
if [ $core_variable_indirectValue_result -ne 0 ]; then
exitCode=1
return 0
fi
index=$((index + 1))
done
}