如果标准输出上有任何输出,如何在标准输出之前打印条件标题

sil*_*ein 7 shell pipe email text-processing

我有一个从目录中过滤文件列表的进程(find检查是否有超过特定时间段的文件以显示队列卡住了)。它可能会也可能不会返回任何东西,如果确实如此,则将其邮寄给cron.

我想知道的是,是否有一个程序或简单的bash方法可以将此输出通过管道传输到其中,并且如果有任何输出通过它,则在前面添加一个标题行(例如“这些是卡在队列中的文件”)。

Jod*_*e C 8

sed 内联答案:

find ./files -mtime +90|sed '1s/^/Files stuck in queue:\n/'
Run Code Online (Sandbox Code Playgroud)


Chr*_*own 5

由于其他方法实际上并不提供bash方法,正如所要求的那样:

command | while IFS= read -r line; do
    (( i )) || { printf '%s\n' 'These are the files stuck in the queue:' ; (( i++ )) ; }
    printf '%s\n' "${line}"
done
Run Code Online (Sandbox Code Playgroud)

所有使用的命令都是bash.

如果没有输出行,则不会打印任何内容。如果有,将打印标题行,然后输出command.