如何循环遍历目录中的所有文件,运行脚本,并将它们写入其他目录中的匹配文件

How to loop through all files in dir, run script, and write them into matching files in other dir

我在目录中有文件:

mkdir in_dir
echo 'a' > in_dir/file1.txt
echo 'b' > in_dir/file2.txt
echo 'c' > in_dir/file3.txt

mkdir out_dir

我有一个命令,它使用 < 从文件中获取输入并使用 > 将输出写入新文件。我想在 in_dir 中的所有文件上 运行 并将输出写入匹配文件名下的 out_dir

这是第一部分,运行对每个文件执行一个(虚拟)命令。

#!/bin/zs
for file in in_dir/*;
(cat $file ; echo '1')

我想在 out_dir 中添加 'file1.txt',内容为 'a1','file2.txt',内容为 'b1' 等

你有 yourscript 处理信息,如果我理解得很好的话。你可以制作另一个脚本如下:

#!/bin/bash

IFS=$'\n'

for i in `ls -1 ./dir_in` ; do
    ./yourscript "$(cat ./dir_in/$i)" > ./dir_out/$i
done

unset IFS

不要忘记获得此脚本的 chmod +x 许可。