diff options
author | Lyndon Brown <jnqnfe@gmail.com> | 2020-03-07 02:33:08 +0000 |
---|---|---|
committer | Raphaƫl Hertzog <hertzog@debian.org> | 2020-03-13 13:50:35 +0000 |
commit | c54cc2c497977d5234a8ea6eb39deb09f0286546 (patch) | |
tree | 12b768f986760b4cc9d0b7d294c02a3042b86410 | |
parent | 1edc3af346f3b94e8bf8d62c9a4dde3dc8b991d4 (diff) | |
download | vyos-live-build-c54cc2c497977d5234a8ea6eb39deb09f0286546.tar.gz vyos-live-build-c54cc2c497977d5234a8ea6eb39deb09f0286546.zip |
echo: really ensure log messages go to stdout
all echo helpers are used as logging functions with output to go to the
terminal. when used in functions that are designed to return a string
though the message printed would get incorrectly captured.
the previous fix done in e3a987d977aaa417afe74349fa4d97dd6afc1c94 was
stupidly flawed; somehow my testing led me to mistakenly believe that
was adequate, but retesting proves that it was not.
here we create a new FD #3 linked to stdout to output the messages on,
which testing shows works as I had actually intended it.
e.g. here:
```
Foo () { if [ "$1" = "a" ]; then printf "foo\n"; else printf "error\n"; fi; }
```
we get:
```
~$ Foo a
foo
~$ Foo b
error
~$ XX="$(Foo a)"
~$ echo "${XX}"
foo
~$ XX="$(Foo b)"
~$ echo "${XX}"
error
```
and as demonstrated, "error" got incorrectly captured by in the variable
whereas here:
```
exec 3>&1
Foo () { if [ "$1" = "a" ]; then printf "foo\n"; else printf "error\n" >&3; fi; }
```
it is different in the last case:
```
~$ XX="$(Foo b)"
error
~$ echo "${XX}"
```
the error successfully makes it to the terminal, and the variable is an
empty string (with a newline automatically printed).
Gbp-Dch: Short
-rwxr-xr-x | functions/echo.sh | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/functions/echo.sh b/functions/echo.sh index 55d87ac72..31c118239 100755 --- a/functions/echo.sh +++ b/functions/echo.sh @@ -8,13 +8,14 @@ ## This is free software, and you are welcome to redistribute it ## under certain conditions; see COPYING for details. +exec 3>&1 Echo () { STRING="${1}" shift - printf "${STRING}\n" "${@}" >&1 + printf "${STRING}\n" "${@}" >&3 } Echo_debug () @@ -23,7 +24,7 @@ Echo_debug () STRING="${1}" shift - printf "D: ${STRING}\n" "${@}" >&1 + printf "D: ${STRING}\n" "${@}" >&3 fi } @@ -52,7 +53,7 @@ Echo_message () PREFIX="P" fi - printf "${PREFIX}: ${STRING}\n" "${@}" >&1 + printf "${PREFIX}: ${STRING}\n" "${@}" >&3 fi } @@ -62,7 +63,7 @@ Echo_verbose () STRING="${1}" shift - printf "I: ${STRING}\n" "${@}" >&1 + printf "I: ${STRING}\n" "${@}" >&3 fi } @@ -83,7 +84,7 @@ Echo_file () { while read -r LINE do - echo "${1}: ${LINE}" >&1 + echo "${1}: ${LINE}" >&3 done < "${1}" } |