简体   繁体   中英

creating a bash script - loop through files

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

我不打算写一个循环 - find实用程序可以为你完成:

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

默认情况下, indent将使用修订的源覆盖输入文件,因此:

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

This should work:

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

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

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