creating a bash script - loop through files
I was wondering, I n开发者_StackOverflow中文版eed 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
I wouldn't bother writing a loop - the find
utility could do it for you already:
find . -name \*.[ch] -print0 | xargs -0 indent ....
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
By default, indent
overwrites the input file(s) with the revised source, hence:
indent -cp33 -di16 -fc1 -fca -hnl -i4 *.c *.h
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 :-)
This should work:
for i in *.c *.h; do
indent "$i" -cp33 -di16 -fc1 -fca -hnl -i4 -o "$i"
done
精彩评论