缩进 Bash 脚本输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1670577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Indenting Bash Script Output
提问by Topher Fangio
I am attempting to write a bash script and I am having difficulty making the output look neat and organized. I could fall back on just using newlines, but I would much rather have output that was easy to read. For instance, when I run git clone ..., I want to first echo "Cloning repository" and then have the output of gitindented. Example output:
我正在尝试编写一个 bash 脚本,但我很难使输出看起来整洁有序。我可以只使用换行符,但我更希望输出易于阅读。例如,当我运行时git clone ...,我想首先回显“克隆存储库”,然后git缩进输出。示例输出:
Cloning repository...
Initialized empty Git repository in /root/client_scripts/jojo/.git/
remote: Counting objects: 130, done.
remote: Compressing objects: 100% (121/121), done.
remote: Total 130 (delta 13), reused 113 (delta 6)
Receiving objects: 100% (130/130), 176.07 KiB, done.
Resolving deltas: 100% (13/13), done.
Currently, it's all compressed with no indentation. Does anyone know how to do this? I attempted with sedand awkbut it didn't seem to show any more output than just Initialized empty Git repository in /root/client_scripts/jojo/.git/. I would greatly appreciate any comments.
目前,它全部被压缩,没有缩进。有谁知道如何做到这一点?我尝试使用sedandawk但它似乎没有显示更多的输出,而不仅仅是Initialized empty Git repository in /root/client_scripts/jojo/.git/. 我将不胜感激任何评论。
采纳答案by CB Bailey
The problem with piping the output of git through any command is that git will detect that the output is not a terminal so it won't output messages which are progress messages because (typically) it is not useful to pipe a whole lot of terminal characters and progress updates to something that isn't a terminal.
通过任何命令管道输出 git 的问题在于,git 会检测到输出不是终端,因此它不会输出作为进度消息的消息,因为(通常)管道传输大量终端字符是没有用的和进度更新到不是终端的东西。
To get the progress messages anyway you need to provide the --verboseoption to git clone. The progress messages appear on stderrso you are likely to need a pipe something like 2>&1 | ....
要获得进展的消息,反正你需要提供的--verbose选项git clone。进度消息出现,stderr因此您可能需要类似2>&1 | ....
Be aware the the progress messages won't appear line by line, but you'll get a lot of terminal escape codes which are designed to clear the same line. Trying to indent this output by piping through a line based tool like sed is likely to prove difficult, if not impossible. For a program that can handles input unbuffered, it should be fairly possible to look for a ^Min the output and add some spaces (or a tab) immediately aftwards, flushing as often as each batch of data is received.
请注意,进度消息不会逐行显示,但您会得到许多旨在清除同一行的终端转义码。如果不是不可能的话,试图通过管道通过像 sed 这样的基于行的工具来缩进这个输出很可能被证明是困难的。对于可以处理无缓冲输入的程序,应该相当有可能^M在输出中查找 a并立即向后添加一些空格(或制表符),在接收到每批数据时刷新。
回答by Joey
Pipe through
管道通过
sed "s/^/ /g"
This will replace the (zero-width) anchor for line start by four spaces, effectively adding four spaces at the start of the line.
这将用四个空格替换行首的(零宽度)锚点,有效地在行首添加四个空格。
(The g does this globally; without it, it will only do it once, which would do the first line.)
( g 在全局范围内执行此操作;没有它,它只会执行一次,这将执行第一行。)
回答by Juliano
A different solution that doesn't require sed:
不需要 sed 的不同解决方案:
command | (while read; do echo " $REPLY"; done)
回答by hlovdal
You can filter the output from the command you want to indent through sed.
您可以通过 sed 过滤要缩进的命令的输出。
/tmp/test>cat script
#!/bin/sh
echo "Running ls -l"
ls -l 2>&1 | sed 's/^/\t/'
/tmp/test>sh script
Running ls -l
total 4
-rw-rw-r-- 1 hlovdal hlovdal 55 2009-11-03 23:36 script
/tmp/test>
The sed command will replace the beginning of the line (before the first character) with a tabulator, i.e. insert a tabulator at the very beginning of the line.
sed 命令将用制表符替换行的开头(第一个字符之前),即在行的开头插入制表符。
Updated to also indent stderr.
更新为还缩进 stderr。
回答by Mark Rushakoff
Since the awk solution wasn't posted yet:
由于 awk 解决方案尚未发布:
$ echo -en "hello\nworld\n"
hello
world
$ echo -en "hello\nworld\n" | awk '{print " "##代码##}'
hello
world

