Pretty-print for shell script

2019-02-10 05:58发布

问题:

I'm looking for something similiar to indent but for (bash) scripts. Console only, no colorizing, etc.

Do you know of one ?

回答1:

Vim can indent bash scripts. But not reformat them before indenting.
Backup your bash script, open it with vim, type gg=GZZ and indent will be corrected. (Note for the impatient: this overwrites the file, so be sure to do that backup!)

Though, some bugs with << (expecting EOF as first character on a line) e.g.

EDIT: ZZ not ZQ



回答2:

A bit late to the party, but it looks like shfmt could do the trick for you.



回答3:

In bash I do this:

source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
declare -f Zibri| cut -c 5-|head --lines=-1|tail --lines=+3

this eliminates comments and reindents the script "bash way". it will not work if the script contains HEREDOCS but if you do this:

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

it will work with any script but the whole script will be indented by 4 spaces. feel free to modify but cite my name in your script and post it! :D



回答4:

Found this http://www.linux-kheops.com/doc/perl/perl-aubert/fmt.script .

Very nice, only one thing i took out is the [...]->test substitution.