简体   繁体   中英

How to write a shell script that indents all files C/C++ files in the current directory?

I am trying to run indent -kr -i8 on all files in my current directory. Being a C programmer, the idea that came to my mind was to fork processes equal to the number of fles, the run exec on them. But I know things can be simplified with shell scripts. Any kind of help is appreciated.

Can you not just:

indent -kr -i8 *.c

You mention forking processes, so if you wanted to do it concurrently:

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

But that will trash the cpu if you've got a load of files. So in batches:

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

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

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

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

launch a process that explicitly calls programs like AStyle or bcpp Astyle code formatting tool with GnuParallel

But is it really necessary to fork so much processes for a so simple and fast task?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM