fork download
  1. echoerr() { printf "%s\n" "$*" >&2; }
  2.  
  3. dump_string_char()
  4. {
  5. local string len_str idx this_char this_int
  6. string="$1"
  7. echoerr "string: \"\"\"$string\"\"\""
  8. len_str="${#string}"
  9. idx=0
  10. while [ $idx -lt ${len_str} ]; do
  11. this_char="${string:$idx:1}"
  12. this_int="$(LC_CTYPE=C printf "%d" "'$this_char")"
  13. echoerr "idx: $idx"
  14. if [ $this_int -lt 32 ]; then
  15. echoerr "$idx: ${this_int}"
  16. else
  17. echoerr "$idx: \"${this_char}\""
  18. fi
  19. ((idx++))
  20. done
  21. }
  22.  
  23. second_function()
  24. {
  25. local second_ini_buffer result2_buffer
  26. second_ini_buffer="$1"
  27.  
  28. # Some magical awk/sed that did not match any pattern
  29. # So let us use 'echo' to re-save the same string
  30.  
  31. IFS= read -rd '' result2_buffer < <(echo "$second_ini_buffer")
  32. echoerr "result2_buffer len: ${#result2_buffer}"
  33. echoerr "\"\"\"$result2_buffer\"\"\""
  34. # so pass back the full ini_buffer as-is.
  35. # hopefully with ONE chr(1) at end-of-line.
  36. # but it doesn't.
  37.  
  38. echo "$result2_buffer"
  39. }
  40.  
  41.  
  42. first_function()
  43. {
  44. local first_buffer result1_buffer
  45. # takes a string with a single chr(10)
  46. first_buffer="$1"
  47.  
  48. # calls a function, which does nothing.
  49. IFS= read -rd '' result1_buffer < <(second_function "$first_buffer")
  50. # IFS= read -rd '' result1_buffer < <(echo "$first_buffer")
  51. # yet it got prepended by another chr(10) for a total of two chr(10)
  52.  
  53. echoerr "result1_buffer len: ${#result1_buffer}"
  54. echoerr "\"\"\"$result1_buffer\"\"\""
  55. # Suuposedly only one way to return a multi-line string neatly,
  56. # and that is via STDOUT (fd=1)
  57. # echo "first_buffer len: ${#first_buffer}"
  58. echo "$result1_buffer"
  59. }
  60.  
  61.  
  62. # there is exactly one chr(10) at the end of ini_buffer
  63. ini_buffer="1.2.3.4
  64. "
  65. echoerr "initial ini_buffer \"\"\"$ini_buffer\"\"\""
  66. echoerr "initial ini_buffer len: ${#ini_buffer}"
  67. echoerr
  68. dump_string_char "$ini_buffer"
  69.  
  70.  
  71. IFS= read -rd '' result < <(first_function "$ini_buffer")
  72. # got a SECOND chr(10) prepended to the final output
  73. # for a total of 3 prepended chr(10)s
  74.  
  75. echoerr "result len: ${#result}"
  76. echoerr "\"\"\"$result\"\"\""
  77. echoerr
  78.  
Success #stdin #stdout #stderr 0.01s 5544KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
initial ini_buffer """1.2.3.4
"""
initial ini_buffer len: 8

string: """1.2.3.4
"""
idx: 0
0: "1"
idx: 1
1: "."
idx: 2
2: "2"
idx: 3
3: "."
idx: 4
4: "3"
idx: 5
5: "."
idx: 6
6: "4"
idx: 7
7: 10
result2_buffer len: 9
"""1.2.3.4

"""
result1_buffer len: 10
"""1.2.3.4


"""
result len: 11
"""1.2.3.4



"""