Bash Script 格式化工具

本文转载至:http://www.arachnoid.com/linux/beautify_bash/

转载说明:一个用于 bashscript 的格式化工具。

Introduction

I wrote a Ruby beautifier script a few years ago and it has become very popular. I decided to rework it to beautify Bash scripts, even though I realized that Bash shell scripts are much more free-form than Ruby programs. This project is the result — a Bash script beautifier written in Ruby.

I have tested the script on a lot of typical Linx scripts, both my own and from the system. While testing I have reluctantly come to the conclusion that there are too many perverse ways people write Bash scripts for this program to ever correctly beautify 100% of submitted scripts. Maybe 99%, but never 100%.

One thing I noticed repeatedly is that people get in a hurry and leave off a terminating token within case ... esac statements, like this:
case $x in
   a)
      # code for a
   ;;
   b)
      # code for b
   ;;
   c)
      # code for c
esac
            

Notice the missing ';;' after "c)"? I found many cases like this, so I created a workaround, since even though it is syntactically incorrect it's obvious that Bash accepts it.

Another common problem, one that I ended up managing on a case-by-case basis, is the frequent use of Bash keywords as variable names, in particular the word "done". I found an example like this:

done=3;echo done;done
            

Obviously this is the work of someone who hates his life and his job, but I managed to process most of these deviant cases, for the same reason — Bash accepts them.

Another perversity is a Bash script than contains within it a huge binary section, somehow grafted onto the script in such a way that a single download contains an installation shell script plus a binary installation block. But the border between the two is not clearly or consistently made, so it's just not possible to process one of these.

So I offer this warning — this program isn't going to be able to process all Bash scripts with equal efficiency, and as to a binary script, it might scramble it. The program makes a backup copy before overwriting the original script, so you can recover in most cases, but do be careful with this script. It is well-behaved and error-free when processing the majority of shell scripts, but there is a small percentage of scripts with such bizarre structure than it can't do a very good job.

To use the script, download the plain-text version below, rename it "beautify_bash.rb", make it executable and put it (or a copy, or a symlink) in /usr/local/bin. Then invoke it in one of these ways:

$ beautify_bash.rb (list of filenames)

$ beautify_bash.rb - < input_file > output_file
            
This script beautifier is Copyright 2008, P. Lutus, and is released under the GPL.
Download
Revision History
  • Version 1.2 09/10/2009. Fixed a user-reported bug having to do with quoted here-doc tags.
  • Version 1.1 10/22/2008. Added some error-printing code for the all-too-common case of unbalanced tokens.
  • Version 1.0 10/22/2008. Initial Public Release.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值