bash 脚本的缩进输出

indent output of bash script

我正在编写一段具有多个输出的代码。我输出东西的部分有两个功能,第一个输出一些缩进消息。我希望第二个函数从前一个输出的缩进级别开始打印一些东西。所以第二个函数应该知道第一个的缩进级别。我该如何实现?

    outputfunction1
    outputfunction2

相对于

    outputfunction1
outputfunction2

谢谢大家

这是一个有趣(但很糟糕)的想法。

#!/bin/sh                                                    

function1() { printf '\t\toutputfunction1\n'; }                
function2() { echo bar; }                                      
indent (){                                                     
        indent=$(printf '%s' "$(for ((i=; $i>0; i--)); do printf '\t'; done)")                                               
        shift                                                  
        $@ | sed "s/^/$indent/"                                
}                                                              

get_indent() {                                                 
        # Call a function and return the indentation of the last line.
        $@ | awk '1; END{ exit( match([=10=], "[^\t]") -1 )}'    
}                                                              


get_indent function1                                           
indent $? function2

请注意,这依赖于 "indent" 被定义为硬选项卡的数量。我将把它留作 reader 的练习,以对任意空格执行此操作。