0

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
Reinstate Monica
  • 663
  • 8
  • 21
  • 2
    What prompt? Sed doesn't ever prompt as far as I know. What sed command? What `read` command? We can't help you debug a command if you don't show it to us. Please [edit] your question and clarify. – terdon Jan 26 '20 at 13:15
  • @terdon I've added clarification/code – Reinstate Monica Jan 26 '20 at 17:50
  • @Kusalananda: I prefer having a wrapper function that I can use generically to indent when I wish to. Some times the command may be executed when I don't want to indent it. It's best to avoid sprinkling every single command with its own indentation. – Reinstate Monica Jan 26 '20 at 17:52
  • 2
    Prompts are usually output on stderr, which is why sed doesn't process them. – glenn jackman Jan 26 '20 at 21:35
  • @glennjackman Even if I `printf` the prompt without the newline it doesn't help. As to be expected, I believe. – Reinstate Monica Jan 26 '20 at 21:45

0 Answers0