indent_find.sh 通过find命令,优点:代码简单,缺点:没有显示正在整理的文件
| 1 | #!/bin/sh |
| 2 | ################################################################################# |
| 3 | #Filename:IndentC/C++codeaccordingtoK&R |
| 4 | #Author:SunYubo |
| 5 | #Created:2010-1-25 |
| 6 | #Description:IndentC/C++codeaccordingtoK&Rbyfind |
| 7 | #Usage:indent.sh[PATH] |
| 8 | #"indet.sh"willindentfileatthePATHyougived. |
| 9 | #Thecurrentdirectorybydefault. |
| 10 | ################################################################################ |
| 11 | |
| 12 | WORK_PATH=$1 |
| 13 | if[-z"$WORK_PATH"];then |
| 14 | echo"IndentCfilesincurrentdirectory?Pleaseinputyorn" |
| 15 | readFLAG |
| 16 | if["$FLAG"=="y"];then |
| 17 | WORK_PATH="." |
| 18 | else |
| 19 | echo"Inputerror.Pleaserunscriptagain." |
| 20 | exit1; |
| 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-typef-iname"*.[c,h]"|xargsindent$OPT |
| 27 |
indent_rec.sh 通过函数实现递归整理,优点:显示正在整理的文件,缺点:编写递归函数
| 1 | #!/bin/sh |
| 2 | ################################################################################# |
| 3 | #Filename:IndentC/C++codeaccordingtoK&R |
| 4 | #Author:SunYubo |
| 5 | #Created:2010-1-25 |
| 6 | #Description:IndentC/C++codeaccordingtoK&Rbyrecursive |
| 7 | #Usage:indent.sh[PATH] |
| 8 | #"indet.sh"willindentfileatthePATHyougived. |
| 9 | #Thecurrentdirectorybydefault. |
| 10 | ################################################################################ |
| 11 | |
| 12 | functionindent_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"IndentingFile:"$cfiles |
| 19 | #indent_kr$cfiles |
| 20 | functionprocessfile(){ |
| 21 | if[-d$1];then |
| 22 | cd$1 |
| 23 | forcurrentfilein`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 |
本文介绍了一种使用shell脚本自动格式化C/C++代码的方法。提供了两种脚本实现方式,一种是利用find命令简单查找并格式化指定路径下的所有.c和.h文件;另一种则是通过递归函数处理目录结构,格式化过程中会显示当前正在处理的文件名。
1704

被折叠的 条评论
为什么被折叠?



