如何使用Shell创建具有不同名称的输出文件?

时间:2019-02-23 01:40:50

标签: linux bash slurm

我有一个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次,但仅生成一个result.txt。但是,我想要多个结果文件,例如result1.txtresult2.txtresult3.txt等。

1 个答案:

答案 0 :(得分:5)

由于迭代编号($i)作为参数从example.sh传递到multiple.sh,因此每次迭代都可以使用它来创建文件。为此,将example.sh的最后一行更改为:

Rscript /home1/R_scripts/abc.R   > "result${1}.txt"
相关问题