整理代码脚本

本文介绍了一种使用shell脚本自动格式化C/C++代码的方法。提供了两种脚本实现方式,一种是利用find命令简单查找并格式化指定路径下的所有.c和.h文件;另一种则是通过递归函数处理目录结构,格式化过程中会显示当前正在处理的文件名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值