shell脚本的漂亮打印

问题描述 投票:6回答:4

我正在寻找类似于indent但是(bash)脚本的东西。仅限控制台,无着色等

你知道吗?

bash scripting shell indentation
4个回答
5
投票

Vim可以缩进bash脚本。但是在缩进之前不要重新格式化它们。 备份你的bash脚本,用vim打开它,键入gg=GZZ并缩进将被更正。 (注意不耐烦:这会覆盖文件,所以一定要做备份!)

虽然,有些错误与<<(期待EOF作为一行的第一个字符),例如

编辑:ZZ不是ZQ


1
投票

派对有点晚了,但看起来shfmt可以为你做这个伎俩。


1
投票

在bash中我这样做:

reindent() {
source <(echo "Zibri () {";cat "$1"; echo "}")
declare -f Zibri|head --lines=-1|tail --lines=+3 | sed -e "s/^\s\s\s\s//"
}

这消除了评论和重新编写脚本“bash方式”。

如果您的脚本中有HEREDOCS,则它们会被前一个函数中的sed破坏。

所以使用:

reindent() {
source <(echo "Zibri () {";cat "$1"; echo "}")
declare -f Zibri|head --lines=-1|tail --lines=+3"
}

但是你的所有脚本都有4个空格缩进。


-1
投票

发现这个http://www.linux-kheops.com/doc/perl/perl-aubert/fmt.script

非常好,我拿出的一件事就是[...] - >测试替换。

© www.soinside.com 2019 - 2024. All rights reserved.