2

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

0

5 Answers 5

6

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

find . -name \*.[ch] -print0 | xargs -0 indent ....
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

indent -cp33 -di16 -fc1 -fca -hnl -i4  *.c *.h

Comments

0

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 :-)

Comments

0

This should work:

for i in *.c *.h; do
    indent "$i" -cp33 -di16 -fc1 -fca -hnl -i4  -o "$i"
done

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.