echoerr() { printf "%s\n" "$*" >&2; }
dump_string_char()
{
local string len_str idx this_char this_int
string="$1"
echoerr "string: \"\"\"$string\"\"\""
len_str="${#string}"
idx=0
while [ $idx -lt ${len_str} ]; do
this_char="${string:$idx:1}"
this_int="$(LC_CTYPE=C printf "%d" "'$this_char")"
echoerr "idx: $idx"
if [ $this_int -lt 32 ]; then
echoerr "$idx: ${this_int}"
else
echoerr "$idx: \"${this_char}\""
fi
((idx++))
done
}
second_function()
{
local second_ini_buffer result2_buffer
second_ini_buffer="$1"
# Some magical awk/sed that did not match any pattern
# So let us use 'echo' to re-save the same string
IFS= read -rd '' result2_buffer < <(echo "$second_ini_buffer")
echoerr "result2_buffer len: ${#result2_buffer}"
echoerr "\"\"\"$result2_buffer\"\"\""
# so pass back the full ini_buffer as-is.
# hopefully with ONE chr(1) at end-of-line.
# but it doesn't.
echo "$result2_buffer"
}
first_function()
{
local first_buffer result1_buffer
# takes a string with a single chr(10)
first_buffer="$1"
# calls a function, which does nothing.
IFS= read -rd '' result1_buffer < <(second_function "$first_buffer")
# IFS= read -rd '' result1_buffer < <(echo "$first_buffer")
# yet it got prepended by another chr(10) for a total of two chr(10)
echoerr "result1_buffer len: ${#result1_buffer}"
echoerr "\"\"\"$result1_buffer\"\"\""
# Suuposedly only one way to return a multi-line string neatly,
# and that is via STDOUT (fd=1)
# echo "first_buffer len: ${#first_buffer}"
echo "$result1_buffer"
}
# there is exactly one chr(10) at the end of ini_buffer
ini_buffer="1.2.3.4
"
echoerr "initial ini_buffer \"\"\"$ini_buffer\"\"\""
echoerr "initial ini_buffer len: ${#ini_buffer}"
echoerr
dump_string_char "$ini_buffer"
IFS= read -rd '' result < <(first_function "$ini_buffer")
# got a SECOND chr(10) prepended to the final output
# for a total of 3 prepended chr(10)s
echoerr "result len: ${#result}"
echoerr "\"\"\"$result\"\"\""
echoerr