繁体   English   中英

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

[英]creating a bash script - loop through files

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

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

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

我怎么能在bash脚本中这样做,所以下次我可以运行脚本并立即进行所有缩进?

谢谢

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

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

我是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

这应该工作:

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

这是一个:

#!/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}/* .

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

暂无
暂无

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

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