简体   繁体   English

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

[英]creating a bash script - loop through files

I was wondering, I need to run indent with a bunch of parameters as: 我想知道,我需要使用一堆参数运行indent

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. 我想要的是读取每个*.c*.h文件并用相同的名称覆盖它们。

How could I do this in a bash script, so next time I can run the script and do all the indentation at once? 我怎么能在bash脚本中这样做,所以下次我可以运行脚本并立即进行所有缩进?

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: 我是Carl的回答 ,但是如果你觉得需要使用循环:

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: 然后 ,检查以确保newdir/中的所有新文件都正常,然后再手动将它们复制回原件:

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 :-) 我不在乎我写脚本多久了,我总是认为我的第一次尝试会搞砸并且可能会丢弃我的文件:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM