indent_find.sh 通过find命令,优点:代码简单,缺点:没有显示正在整理的文件
1 | #!/bin/sh |
2 | ################################################################################# |
3 | # Filename: Indent C/C++ code according to K&R |
4 | # Author: Sun Yubo |
5 | # Created: 2010-1-25 |
6 | # Description: Indent C/C++ code according to K&R by find |
7 | # Usage: indent.sh [PATH] |
8 | # "indet.sh" will indent file at the PATH you gived. |
9 | # The current directory by default. |
10 | ################################################################################ |
11 | |
12 | WORK_PATH=$1 |
13 | if [ -z "$WORK_PATH" ]; then |
14 | echo "Indent C files in current directory ? Please input y or n" |
15 | read FLAG |
16 | if [ "$FLAG" == "y" ]; then |
17 | WORK_PATH="." |
18 | else |
19 | echo "Input error. Please run script again." |
20 | exit 1; |
21 | fi |
22 | fi |
23 | |
24 | OPT="-nbad -bap -bbo -nbc -br -brs -c33 -cd33 -ncdb -ce -ci8 -cli8 -cp33 -cs -d0 -di0 -nfc1 -nfca -hnl -i8 -ip0 -lp -pcs -nprs -psl -saf -sai -saw -cdb -sc -nsob -nss -npro -ts8 -sob -l80 -ss -bli0 -bfda -ppi3 -bs -T -v" |
25 | |
26 | find $WORK_PATH -type f -iname "*.[c,h]" | xargs indent $OPT |
27 |
indent_rec.sh 通过函数实现递归整理,优点:显示正在整理的文件,缺点:编写递归函数
1 | #!/bin/sh |
2 | ################################################################################# |
3 | # Filename: Indent C/C++ code according to K&R |
4 | # Author: Sun Yubo |
5 | # Created: 2010-1-25 |
6 | # Description: Indent C/C++ code according to K&R by recursive |
7 | # Usage: indent.sh [PATH] |
8 | # "indet.sh" will indent file at the PATH you gived. |
9 | # The current directory by default. |
10 | ################################################################################ |
11 | |
12 | function indent_kr() { |
13 | `indent -nbad -bap -bbo -nbc -br -brs -c33 -cd33 -ncdb -ce -ci8 -cli8 -cp33 -cs -d0 -di0 -nfc1 -nfca -hnl -i8 -ip0 -lp -pcs -nprs -psl -saf -sai -saw -cdb -sc -nsob -nss -npro -ts8 -sob -l80 -ss -bli0 -bfda -ppi3 -bs -T -v $1` |
14 | } |
15 | #if [ -z $cfiles ]; then |
16 | # echo -e "" |
17 | # else |
18 | # echo -e "Indenting File:"$cfiles |
19 | # indent_kr $cfiles |
20 | function processfile() { |
21 | if [ -d $1 ]; then |
22 | cd $1 |
23 | for currentfile in `ls` |
24 | do |
25 | if [ -d $currentfile ]; then |
26 | echo -e "Dir:$currentfile" |
27 | processfile $currentfile |
28 | elif [ -f $currentfile ]; then |
29 | cfiles=$(echo $currentfile | grep '/.[c,h]$') |
30 | if [ "$cfiles" != "" ]; then |
31 | echo -e "Indenting:"$cfiles |
32 | indent_kr $cfiles |
33 | fi |
34 | fi |
35 | done |
36 | cd .. |
37 | fi |
38 | } |
39 | |
40 | processfile $1 |
41 |