I'm trying to indent the output of 'subcommands' in a script, like this:
Turning off image...
Unmounting...done.
Detaching loop device...done
where the indented items are individual steps executed during Turning off image....
The standard solution is to use sed et. al. like in this question however some of the steps in my script require prompting the user to confirm an action using read. Since the prompt is not terminated in a newline, it is not output by sed until after the read command completes. Is there a way around this?
Clarification: One of the subcommands itself is calling read with a prompt. When the subcommand is piped through sed, sed won't output the prompt because it is not yet terminated with a newline.
Here's example code (somewhat contrived to get the point across):
function indent () { (set -o pipefail; "$@" 2>&1 | sed 's/^/ /';); }
function some_cleanup () {
# ... some commands
read -rp "Do you want to force a dismount? (y/n)" yesno
if [ "$yesno" = "yes" ]; then force_dismount; fi
}
indent some_cleanup