【问题标题】:How to loop through all files in dir, run script, and write them into matching files in other dir如何遍历目录中的所有文件,运行脚本,并将它们写入其他目录中的匹配文件
【发布时间】:2021-02-03 19:40:50
【问题描述】:

我的目录中有文件:

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' 等等。

【问题讨论】:

  • echo "$(cat $file)1" > "out_dir/$file"
  • out_dir/file1.txt的内容是什么? aa1?
  • a1,我承认这不是很清楚

标签: bash zsh


【解决方案1】:

如果我理解得很好,你有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 的权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    相关资源
    最近更新 更多