如何编写一个shell脚本来缩进当前目录中的所有文件C / C ++文件?

时间:2012-03-23 12:35:29

标签: c bash indentation

我正在尝试对当前目录中的所有文件运行indent -kr -i8。作为一名C程序员,我想到的想法是将进程分叉等于fles的数量,运行exec就在它们上面。但我知道shell脚本可以简化一些事情。任何形式的帮助都表示赞赏。

3 个答案:

答案 0 :(得分:5)

你能不能只是:

indent -kr -i8 *.c

你提到分叉过程,所以如果你想同时做:

for f in *.c
do
   indent -kr -i8 $f &
done

但如果您有大量文件,那将会丢弃cpu。所以分批:

limit=10
for f in *.c
do
   indent -kr -i8 $f &
   (( count = count + 1 ))

   if [[ $count -eq $limit ]] then
      wait
      count=0
   fi
done

答案 1 :(得分:4)

使用find,结合xargs及其特殊参数,指定最大进程数,以及为每个进程处理的参数(文件)数。

find -name '*' -type f -print0 | xargs -0 --max-args=1 --max-procs=8 indent -kr -i8

答案 2 :(得分:0)

启动一个显式调用AStyle或bcpp等程序的进程 Astyle code formatting tool与GnuParallel

但真的有必要为这么简单快速的任务分配这么多流程吗?