2010-07-16 70 views
2

我想知道循環,我需要用一大堆的參數運行indent創建一個bash腳本 - 通過文件

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

我想是讀取每個*.c*.h文件,並用覆蓋他們一樣的名字。

我怎麼能在bash腳本中做到這一點,所以下次我可以運行腳本並一次完成所有的縮進?

感謝

+0

完美!謝謝大家。 – Karl 2010-07-16 22:55:30

+2

不要忘記將答案標記爲已接受。 – 2010-07-17 00:08:23

回答

6

我就懶得寫一環 - 在find工具可以爲你做它已經:

find . -name \*.[ch] -print0 | xargs -0 indent .... 
0

這應該工作:

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

我第二Carl's answer,但如果你覺得需要使用循環:

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

這裏有一個:

#!/bin/bash 

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

然後,你檢查,以在newdir/確保所有的新文件都還好你手動複製之前,他們又回到了原稿:

cp ${newdir}/* . 

這最後paragraphe是重要的。我不在乎我一直在寫腳本多久,我總是假設我的第一次嘗試會搞砸,並可能垃圾我的文件:-)

1

默認情況下,indent用修訂後的源覆蓋輸入文件(s) ,因此:

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

...並進行備份。 – 2010-07-17 00:07:27