Bash: Split stdout from multiple concurrent commands into columns

前端 未结 3 1345
一生所求
一生所求 2021-02-06 08:53

I am running multiple commands in a bash script using single ampersands like so:

commandA & commandB & commandC

They each have their ow

3条回答
  •  春和景丽
    2021-02-06 09:22

    pr is a solution, but not a perfect one. Consider this, which uses process substitution (<(command) syntax):

    pr -m -t <(while true; do echo 12; sleep 1; done) \
             <(while true; do echo 34; sleep 2; done)
    

    This produces a marching column of the following:

    12                                  34
    12                                  34
    12                                  34
    12                                  34
    

    Though this trivially provides the output you want, the columns do not advance individually—they advance together when all files have provided the same output. This is tricky, because in theory the first column should produce twice as much output as the second one.

    You may want to investigate invoking tmux or screen in a tiled mode to allow the columns to scroll separately. A terminal multiplexer will provide the necessary machinery to buffer output and scroll it independently, which is important when showing output side-by-side without allowing excessive output from commandB to scroll commandA and commandC off-screen. Remember that scrolling each column separately will require a lot of screen redrawing, and the only way to avoid screen redraws is to have all three columns produce output simultaneously.

    As a last-ditch solution, consider piping each output to a command that indents each column by a different number of characters:

    this is something that commandA outputs and is
        and here is something that commandB outputs
    interleaved with the other output, but visually
    you might have an easier time distinguishing one
            here is something that commandC outputs
        which is also interleaved with the others
    from the other
    

提交回复
热议问题