-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdemo.sh
executable file
·111 lines (90 loc) · 2.21 KB
/
demo.sh
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
#
# Usage:
# ./demo.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
#
# Improving the Style of Shell from Nix Package Manager
#
# from https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh#L318
# Return success if the specified file is an ELF object.
isELF() {
local fn="$1"
local fd
local magic
exec {fd}< "$fn"
read -r -n 4 -u "$fd" magic
exec {fd}<&-
if [[ "$magic" =~ ELF ]]; then return 0; else return 1; fi
}
# My simpler rewrite.
isElfSimple() {
local path=$1 # double quotes never necessary on RHS
local magic
# read 4 bytes from $path, without escaping, into $magic var
read -r -n 4 magic < "$path"
# Return the exit code of [[
[[ "$magic" =~ ELF ]]
}
isElfSimpleWithStdin() {
seq 3 > /tmp/3.txt
while read line; do
echo $line
isElfSimple /bin/true && echo YES
isElfSimple $0 || echo NO
echo
done < /tmp/3.txt
}
compareIsElf() {
local path=$1
isELF "$path" && echo "YES isELF $path" || echo "NO isELF $path"
isElfSimple "$path" && echo "YES isElfSimple $path" || echo "NO isElfSimple $path"
}
testIsElf() {
for path in /bin/sh /bin/true $0; do
compareIsElf "$path"
echo
done
}
#
# Improving the Style of Shell from Apache Yetus
#
doWork() {
echo 'FOO'
echo 'BAR'
echo
}
doWorkAndLog() {
# https://github.com/apache/yetus/blob/10d4d13cc95a814eac97a976a8de525531ac986a/precommit/core.d/builtin-bugsystem.sh#L50
if [[ -n "${CONSOLE_REPORT_FILE}" ]]; then
echo "--- Logging to ${CONSOLE_REPORT_FILE}"
exec 6>&1 1>"${CONSOLE_REPORT_FILE}"
fi
doWork
if [[ -n "${CONSOLE_REPORT_FILE}" ]]; then
exec 1>&6 6>&-
echo "--- Contents of ${CONSOLE_REPORT_FILE}:"
cat "${CONSOLE_REPORT_FILE}"
fi
}
doWorkAndLogSimple() {
if [[ -n "${CONSOLE_REPORT_FILE}" ]]; then
echo "--- Logging to ${CONSOLE_REPORT_FILE}"
doWork > ${CONSOLE_REPORT_FILE}
echo "--- Contents of ${CONSOLE_REPORT_FILE}:"
cat "${CONSOLE_REPORT_FILE}"
else
doWork
fi
}
testDoWorkAndLog() {
set +o nounset
doWorkAndLog
CONSOLE_REPORT_FILE=/tmp/$0-$$.log doWorkAndLog
echo --- SIMPLE VERSION ---
doWorkAndLogSimple
CONSOLE_REPORT_FILE=/tmp/$0-$$.log doWorkAndLogSimple
}
"$@"