当我想在一个 Bash 调用中进行多个输出时,是否有 Bash 包装器 (program/script) 可以实现更简洁的输入

Is there a Bash wrapper (program/script) that enables a more succinct input when I want multiple outputs in one Bash call

我目前正在创造如下怪物:

ll /home && echo -e "==============\n" && getent passwd && echo -e "==============\n" && ll /opt/tomcat/ && echo -e "==============\n" && ll /etc/sudoers.d/

是否有一些程序可以更好地处理这个问题?
像这样(在我的例子中程序的假设名称是 multiprint):

multiprint --delim-escapechars true --delim "============\n" '{ll /home},{getent passwd},...'

或者:

multiprint -de "============\n" '{ll /home},{getent passwd},...'

像下面这样的函数会给你这种能力:

function intersect() {
    delim=
    shift
    for f; do cat "$f"; echo "$delim"; done
}

您可以按如下方式调用它来实现您的特定 use-case :

intersect '==============' <(ll /home) <(getent passwd) <(ll /opt/tomcat/) <(ll /etc/sudoers.d/)

你可以try it here.

printf 将重复其格式,直到其参数用尽。你可以这样写

printf '%s\n================\n' "$(ll /home)" "$(getent passed)" "$(ll /opt/tomcat)" "$(ll /etc/sudoers.d)"

尽管这有点 memory-intensive,因为它会将所有输出缓存在内存中,直到所有命令完成。

根据@Aaron 的回答,我最终创建了这个 multiprint.sh Bash shell 脚本,并且值得在这里发布:

#!/bin/bash
# Print output of multiple commands, delimited by a specified string

function multiprint() {
    if [[ -z "$*" ]]; then
        __multiprint_usage
        return 0
    elif [[ "" == "--help" ]]; then
        __multiprint_usage
        return 0
    else
        delim=
        shift
        for f; do cat "$f"; echo -e "$delim"; done
    fi
}

function __multiprint_usage() {
    echo "Usage:"
    echo "  multiprint '<delimiter>' <(cmd1) <(cmd2) ..."
    # example: multiprint '\n\n\n' <(ll /home/) <(ll /var/) ..."
}