创建一个bash脚本 - 循环遍历文件

时间:2010-07-16 22:46:37

标签: bash loops

我想知道,我需要使用一堆参数运行indent

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

我想要的是阅读每个*.c*.h文件,并用相同的名称覆盖它们。

我怎样才能在bash脚本中执行此操作,因此下次我可以运行脚本并立即执行所有缩进?

由于

5 个答案:

答案 0 :(得分:6)

我不打算写一个循环 - find实用程序可以为你做这件事:

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

答案 1 :(得分:2)

我是第二个Carl's answer,但如果你觉得需要使用循环:

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

答案 2 :(得分:1)

默认情况下,indent会使用修改后的源代码覆盖输入文件,因此:

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

答案 3 :(得分:0)

这应该有效:

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

答案 4 :(得分: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}/* .

最后一段话很重要。我不在乎我写脚本多久了,我总是认为我的第一次尝试会搞砸并且可能会丢弃我的文件: - )

相关问题