'How to execute a script by a script with echos output to screen AND redirection to a variable

I'm exceuting a shell script within another one in a loop depicted here in a general fashion:

#!/bin/bash
counter=$1
while ((counter > 0))
do
  counter=$((counter - 1))
  output=$(user-response | ./myscript.sh paramter1 paramter2)
  
  # some $output - processing

done

I'm collecting the echos of "myscript.sh" in the variable "output" for further processing. The piped user-response is required by myscript during execution. However, for debugging purposes, I'd like to see the echos from the called script "myscript.sh" additionally in real time on the screen, as it hangs up from time to time. I hope to get a hint of the point of the erratic hang ups from the last echo of "myscript.sh" during "embedded" execution.

Just echoing the output variable by the caller does not help, as this requires the called script to come to an end. Most convenient would be to do both in parallel, i.e. having myscript show its echos on the screen and simultaneously collect it into "output". I also thought about reading a number of historic output lines from the terminal. Is this possible? I only managed to read the command history, but no script echos.

How can I do this? I've spent half a day to find a solution with no result.

Any help is very much appreciated. Thank you!



Solution 1:[1]

Wouldn't splitting the operation into steps work?

#!/bin/bash
counter=$1
while ((counter > 0))
do
  counter=$((counter - 1))
  input=[get user-response]
  echo $input
  output=$(echo $input | ./myscript.sh paramter1 paramter2)

  # some $output - processing
done

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wquinoa