linux - 多次运行脚本并将输出保存在多个文件夹中

标签 linux bash shell for-loop

我正在尝试在多个文件夹中运行一个脚本,它调用:一个软件,两个自动创建一个文件夹作为输出的输入文件(在输入文件夹中),例如:

./soft -in1 input1 -in2 input2

此外,我有多个文件夹,如下所示:

├── Folder1
│   ├── input1.in1
│   ├── input1.in2
│   ├── soft.py
├── Folder2
│   ├── input2.in1
│   ├── input2.in2
│   ├── soft.py                                                                             
├── script.sh

所以,我想做两个过程:

首先,递归运行脚本(在所有文件夹中),其次,在每个文件夹中重复运行“X”次脚本。我让这个脚本递归运行。

但是我在尝试运行重复时遇到了问题。我尝试使用 seq 命令,但软件 ./soft 在每次重复时重写输出。因此,我需要将输出保存在包含每次重复次数的文件夹中(例如\out_Folder1_rep1;\out_Folder1_rep2;\out_Folder1_rep3)。

我之前需要为每次重复创建输出文件夹吗?。有人可以帮助我吗?

for dir in */; do
    for r in in1; do
        glob=*.${r}
        "./soft" -in1 "$dir"/$glob -in2 "$dir"/$(basename -s .tpl $glob).in2 ;
    done
done

最佳答案

First, run the script recursively (in all folders), and second, run 'X' repetitions of the script in each folde. I got this script to run recursively.

不是进行递归,而是首先读取根文件夹中所有文件夹的列表,然后“更改目录”到文件夹列表中的每个条目,然后执行您的脚本。

请在下面找到示例代码:

#!/bin/sh

if [ ! -z "${1}" ];
then
    DIR_LIST=$(ls -d "${1}/*")
    for dir in "${DIR_LIST}";
    do
      cd ${dir}
      # run your script here
    done
fi

关于linux - 多次运行脚本并将输出保存在多个文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52087838/

相关文章:

bash/sed,以相同的缩进级别替换文件中的行

linux - 为什么 "while"并发在 shell 脚本中运行越来越慢?

python - 如何在使用 python 在提示符下发出命令后截取 stdout 的每一行

linux - 有没有办法知道某个文件是否已经被复制到U盘中?

linux - 未能创建 map : 22 Invalid argument

regex - 使用 sed 或 awk,如何从当前行的末尾匹配回指定的字符?

bash - 即使在 chown 和 chmod 之后也无法移动或删除文件

linux:智能fsync()?

linux - "bash: nc: command not found"ssh 多跳错误

mysql脚本bash循环