tags:

views:

69

answers:

5

I was wondering, I need to run indent with a bunch of parameters as:

indent slithy_toves.c -cp33 -di16 -fc1 -fca -hnl -i4  -o slithy_toves.c

What I want is to read each *.c and *.h files and overwrite them with the same name.

How could I do this in a bash script, so next time I can run the script and do all the indentation at once?

Thanks

+6  A: 

I wouldn't bother writing a loop - the find utility could do it for you already:

find . -name \*.[ch] -print0 | xargs -0 indent ....
Carl Norum
A: 

This should work:

for i in *.c *.h; do
    indent $i -cp33 -di16 -fc1 -fca -hnl -i4  -o $i
done
WoLpH
+2  A: 

I second Carl's answer, but if you do feel the need to use a loop:

for filename in *.[ch]; do
    indent "$filename" -cp33 -di16 -fc1 -fca -hnl -i4  -o "$filename"
done
Michael Mrozek
A: 

Here's one:

#!/bin/bash

rm -rf newdir
mkdir newdir
for fspec in *.[ch] ; do
    indent "${fspec}" -cp33 -di16 -fc1 -fca -hnl -i4  -o "newdir/${fspec}"
done

Then, you check to make sure all the new files in newdir/ are okay before you copy them back over the originals manually:

cp ${newdir}/* .

That last paragraphe is important. I don't care how long I've been writing scripts, I always assume my first attempt will screw up and possible trash my files :-)

paxdiablo
+1  A: 

By default, indent overwrites the input file(s) with the revised source, hence:

indent -cp33 -di16 -fc1 -fca -hnl -i4  *.c *.h
Jonathan Leffler
...and makes backups.
Dennis Williamson