各シェルスクリプトの出力を保存します。

各シェルスクリプトの出力を保存します。

ループを繰り返すbashスクリプトがあります。繰り返しに基づいて個々のスクリプトの出力をbashファイルに保存する方法は?

#Working directory
cd /lustre/nobackup/WUR/ABGC/agyir001/
    
# Set the number of replicates
num_replicates=100

# Loop over the replicates
for i in $(seq 1 $num_replicates); do
# replicate number with leading zeros
rep_num=$(printf "%03d" $i)

output_file="accuracy_results_${rep_num}.txt"
  
#Run scenario 1,2 and 3 of male contributions

Rscript Scenario_1_male_contributions.R
Rscript Scenario_2_male_contributions.R
Rscript Scenario_3_male_contributions.R

#creating correlated qtl effects for 1000 selected qtls
Rscript Correlated_qtl_effects.R

#Selecting 1000 qtls, creating TBVS and phenotypes
Rscript Breeding_values_and_Phenotypes.R 7500

# prepare phenotypes and genotypes/markers
Rscript /lustre/nobackup/WUR/ABGC/agyir001/scripts/Markers_phenotypes_for_EBV.R

ベストアンサー1

単一コマンドの出力を変数名のファイルに保存するには、output_file通常のリダイレクトを使用できます。

Rscript Scenario_1_male_contributions.R > "$output_file"

またはstdoutとstderrが必要な場合

Rscript Scenario_1_male_contributions.R > "$output_file" 2>&1

複数のコマンドの出力を同じファイルに適用するには、各コマンドのリダイレクトを追加し、毎回ファイルを切り捨てるのではなく、追加>>モードを使用する必要があります。>

ただし、各リダイレクトに個別にリダイレクトを追加するのは少し面倒であるため、コマンドのグループ化を使用できます。

{
Rscript Scenario_1_male_contributions.R
Rscript Scenario_2_male_contributions.R
Rscript Scenario_3_male_contributions.R
} > "$output_file"

ここでは、>グループ全体に対してファイルを一度だけ開くので、通常のリダイレクトは問題ありません。つまり、以前に実行したスクリプトのデータをファイルに保存したくない場合です。


または、ループ内Rscriptの同じファイル内のすべての呼び出しの出力を保存します。

for i in $(seq 1 $num_replicates); do
    # replicate number with leading zeros
    rep_num=$(printf "%03d" $i)
    output_file="accuracy_results_${rep_num}.txt"
  
    {
        #Run scenario 1,2 and 3 of male contributions
        Rscript Scenario_1_male_contributions.R
        Rscript Scenario_2_male_contributions.R
        Rscript Scenario_3_male_contributions.R

        Rscript ...
    } > "$output_file"
done

おすすめ記事