简体   繁体   English

Bash:将stdout从多个并发命令拆分为列

[英]Bash: Split stdout from multiple concurrent commands into columns

I am running multiple commands in a bash script using single ampersands like so: 我使用单个&符号在bash脚本中运行多个命令,如下所示:

commandA & commandB & commandC

They each have their own stdout output but they are all mixed together and flood the console in an incoherent mess. 它们每个都有自己的stdout输出,但它们都混合在一起,使控制台泛滥成熟。

I'm wondering if there is an easy way to pipe their outputs into their own columns... using the column command or something similar. 我想知道是否有一种简单的方法将输出管道输入到他们自己的列中...使用column命令或类似的东西。 ie. 即。 something like: 就像是:

commandA | column -1 & commandB | column -2 & commandC | column -3

New to this kind of thing, but from initial digging it seems something like pr might be the ticket? 这种事情的新手,但从最初的挖掘看起来似乎pr可能是票? or the column command...? column命令......?

Regrettably answering my own question. 遗憾地回答了我自己的问题。

None of the supplied solutions were exactly what I was looking for. 所提供的解决方案都不是我想要的。 So I developed my own command line utility: multiview . 所以我开发了自己的命令行实用程序: multiview Maybe others will benefit? 也许其他人会受益?

It works by piping processes' stdout/stderr to a command interface and then by launching a "viewer" to see their outputs in columns: 它通过将进程的stdout / stderr管道连接到命令接口,然后通过启动“viewer”来查看它们在列中的输出:

fooProcess | multiview -s & \
barProcess | multiview -s & \
bazProcess | multiview -s & \
multiview

This will display a neatly organized column view of their outputs. 这将显示其输出的整齐有序的列视图。 You can name each process as well by adding a string after the -s flag: 您可以通过在-s标志后添加字符串来命名每个进程:

fooProcess | multiview -s "foo" & \
barProcess | multiview -s "bar" & \
bazProcess | multiview -s "baz" & \
multiview

There are a few other options, but thats the gist of it. 还有其他一些选择,但这就是它的要点。

Hope this helps! 希望这可以帮助!

pr is a solution, but not a perfect one. pr是一个解决方案,但不是一个完美的解决方案。 Consider this, which uses process substitution ( <(command) syntax): 考虑一下,它使用进程替换<(command)语法):

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. 您可能希望调查以平铺模式调用tmuxscreen以允许列单独滚动。 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. 终端多路复用器将提供必要的机制来缓冲输出并独立滚动它,这在并排显示输出时很重要,而不允许commandB的过多输出滚动commandA和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

Script print out three vertical rows and a timer each row containing the output from a single script. 脚本打印出三个垂直行和一个计时器,每行包含一个脚本的输出。 Comment on anything you dont understand and ill add answers to my answer as needed 评论你不理解的任何内容,并根据需要添加答案

Hope this helps :) 希望这可以帮助 :)

#!/bin/bash
#Script by jidder

count=0
Elapsed=0
control_c()
{
    tput rmcup
    rm tail.tmp
    rm tail2.tmp
    rm tail3.tmp
    stty sane
}


Draw()
{
       tput clear
       echo "SCRIPT 1                                                                                                                     Elapsed time =$Elapsed seconds"
        echo "------------------------------------------------------------------------------------------------------------------------------------------------------"
        tail -n10 tail.tmp
        tput cup 25 0

        echo "Script 2                                                                                                                                                   "
        echo "------------------------------------------------------------------------------------------------------------------------------------------------------"
        tail -n10 tail2.tmp
        tput cup 50 0

        echo "Script 3                                                                                                                                                   "
        echo "------------------------------------------------------------------------------------------------------------------------------------------------------"
        tail -n10 tail3.tmp
}


Timer()
{
        if [[ $count -eq 10  ]]; then
                Draw
                ((Elapsed = Elapsed + 1))
                count=0
        fi


}
main()
{
    stty -icanon time 0 min 0
    tput smcup
    Draw
    count=0
    keypress=''
    MYSCRIPT1.sh > tail.tmp &
    MYSCRIPT2.sh > tail2.tmp &
    MYSCRIPT3.sh > tail3.tmp &

    while [ "$keypress" != "q" ]; do
            sleep 0.1
            read keypress
            (( count = count + 2 ))
            Timer
    done

    stty sane
    tput rmcup
    rm tail.tmp
    rm tail2.tmp
    rm tail3.tmp
    echo "Thanks for using this script."
    exit 0
}

main

trap control_c SIGINT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM