脚本一:格式化指定目录下的源文件(*.h, *.cpp...)
#!/bin/sh # 格式化某目录下所有*.h, *.c, *.cpp, *.hh文件, 并将文件换行符转换成Linux下的格式 if [ $# -lt 1 ]; then echo "Usage: $0 <dir>" exit 1 else dir=$1 fi # format a source file(*.c, *.h, *.cpp, *.hh) formatSrcfile() { dos2unix $1 indent -npro -nip -lp -npsl -npcs -i4 -ts4 -sob -l140 -ci4 -ss -nsaf -nsai -nsaw -bl -bli0 $1 rm -f "$1"~ } # save file path to file find $dir -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.hh' > tmp # format every file in tmp while read file do formatSrcfile $file done < tmp # remove tmp file rm -f tmp
脚本二:格式化指定的文件
#!/bin/sh # 格式化指定的文件, 并将文件换行符转换成Linux下的格式 if [ $# -lt 1 ]; then echo "Usage: $0 file1 file2 file*.cpp ..." exit 1 fi # format a source file(*.c, *.h, *.cpp, *.hh) formatSrcfile() { dos2unix $1 indent -npro -nip -lp -npsl -npcs -i4 -ts4 -sob -l140 -ci4 -ss -nsaf -nsai -nsaw -bl -bli0 $1 rm -f "$1"~ } for i in $* do formatSrcfile $i done
因为indent的参数很多,我这里用的格式不一定是所有人都喜欢的,所以如果不符合您的要求,请大家自己修改。
我这两个脚本都有一个bug,一直没找到解决办法,若有人解决了,还望告知下。bug如下:
就是上图左边的代码,会被格式成右边的样子,但是事实上对于左边这样的格式,我希望他能保持不变,不知如何修改indent参数呢?
另外,如果一个宏单独一行,且没有分号作结尾,他会将下一行的内容给捞上来的,哈哈!