首页 > 解决方案 > 如何使用 Shell 创建具有不同名称的输出文件?

问题描述

我有一个 R 脚本(abc.R):

#!/usr/bin/env Rscript

print("HELLO")

还有一个包含 R 脚本 ( example.sh) 的批处理脚本:

#!/bin/bash
module load Rstats
module load RstatsPackages


Rscript /home1/R_scripts/abc.R   > "result.txt"

还有另一个批处理脚本 ( multiple.sh),它调用上述脚本:

#!/bin/sh

for((i=1;i<=10;i++))
do

     sbatch -p normal -t 4:00:00 -N 1 -n 4 example.sh $i

done

sh multiple.sh

该脚本调用上述脚本十次,这样我的 Rscript 将运行 10 次。它运行了 10 次,但只生成一个 result.txt。但是,我想要多个结果文件,例如,result1.txt等。result2.txtresult3.txt

标签: linuxbashslurm

解决方案


由于迭代号 ( $i) 作为参数传递给example.shfrom multiple.sh,因此可以使用相同的方法在每次迭代时创建一个文件。为此,将最后一行更改example.sh为:

Rscript /home1/R_scripts/abc.R   > "result${1}.txt"

推荐阅读