bash中有两个嵌套循环的问题

时间:2013-09-09 21:58:08

标签: bash loops for-loop sed nested-loops

我试图循环两个系列的变量并用R统计软件制作它们以获得相关结果。

我很困惑,为什么每次内循环结束时都不会发生更大的循环(在mica_headers上)。

#!/bin/sh
#set -e


micaHeaderList="tot_instruction ILP32 ILP64 ILP128 ILP256 total_ins_count_for_hpc_alignment totInstruction mem-read mem-write control-flow arithmetic floating-point stack shift string sse other nop InstrFootprint64 InstrFootprint4k DataFootprint64 DataFootprint4k mem_access memReuseDist0-2 memReuseDist2-4 memReuseDist4-8 memReuseDist8-16 memReuseDist16-32 memReuseDist32-64 memReuseDist64-128 memReuseDist128-256 memReuseDist256-512 memReuseDist512-1k memReuseDist1k-2k memReuseDist2k-4k memReuseDist4k-8k memReuseDist8k-16k memReuseDist16k-32k memReuseDist32k-64k memReuseDist64k-128k memReuseDist128k-256k memReuseDist256k-512k memReuseDist512k-00 GAg_mispred_cnt_4bits PAg_mispred_cnt_4bits GAs_mispred_cnt_4bits PAs_mispred_cnt_4bits GAg_mispred_cnt_8bits PAg_mispred_cnt_8bits GAs_mispred_cnt_8bits PAs_mispred_cnt_8bits GAg_mispred_cnt_12bits PAg_mispred_cnt_12bits GAs_mispred_cnt_12bits PAs_mispred_cnt_12bits total_brCount total_transactionCount total_takenCount total_num_ops instr_reg_cnt total_reg_use_cnt total_reg_age reg_age_cnt_1 reg_age_cnt_2 reg_age_cnt_4 reg_age_cnt_8 reg_age_cnt_16 reg_age_cnt_32 reg_age_cnt_64 mem_read_cnt mem_read_local_stride_0 mem_read_local_stride_8 mem_read_local_stride_64 mem_read_local_stride_512 mem_read_local_stride_4096 mem_read_local_stride_32768 mem_read_local_stride_262144 mem_read_global_stride_0 mem_read_global_stride_8 mem_read_global_stride_64 mem_read_global_stride_512 mem_read_global_stride_4096 mem_read_global_stride_32768 mem_read_global_stride_262144 mem_write_cnt mem_write_local_stride_0 mem_write_local_stride_8 mem_write_local_stride_64 mem_write_local_stride_512 mem_write_local_stride_4096 mem_write_local_stride_32768 mem_write_local_stride_262144 mem_write_global_stride_0 mem_write_global_stride_8 mem_write_global_stride_64 mem_write_global_stride_512 mem_write_global_stride_4096 mem_write_global_stride_32768 mem_write_global_stride_262144"
mhToBeReplaced="ILP32"


compilerOptionList="funsafe_math_optimizations fno_guess_branch_probability fno_ivopts fno_tree_loop_optimize fno_inline_functions funroll_all_loops fno_omit_frame_pointer falign_jumps fselective_scheduling fno_inline_small_functions fno_tree_pre ftracer fno_move_loop_invariants"
coToBeReplaced="fno_guess_branch_probability"

for mica_header in $micaHeaderList
do

    for compiler_option in $compilerOptionList
    do

        echo "Calculating $compiler_option correlation for $mica_header"
        sed -i "s/$coToBeReplaced/$compiler_option/g" r.scr
        coToBeReplaced=$compiler_option
        make
    done


    sed -i "s/$mhToBeReplaced/$mica_header/g" r.scr
    mhToBeReplaced=$mica_header

done

编辑:通常,为了避免迭代值和源文件之间的不一致,我怎么能够将这两者从不同的文件链接在一起。即带有ALL.scv文件内标题的micaHeaderList?

1 个答案:

答案 0 :(得分:0)

在脚本中有一个错误,它回显$mica_header的值,它将在下一个外循环运行中仅替换make个调用。这是由sed之后仅运行第二个make引起的。应该在内循环之前调用它。这不能引起混乱吗?

此外,不是将参数的一个值替换为另一个参数值,而是最好创建一个r.src文件模板,其中包含要更改的参数的占位符。通过这种方式,我们可以确定参数的某些值不会与搜索模式发生冲突,而且拼写错误也不会阻止替换。

下面是一个改进的脚本,代码相当简单。 if test "$Test" = 1中的部分只是Test=1激活的测试代码。对于此脚本,只需为名为r.scr的{​​{1}}创建模板,并将占位符r.scr.template%%MicaHeader%%放置,而不是应替换的参数。对于第一个测试,有一个较短的参数列表。

%%CompilerOption%%

如果问题仍然存在:

  1. 检查生成的#!/bin/sh micaHeaderList="tot_instruction ILP32 ILP64" mhToBeReplaced=%%MicaHeader%% compilerOptionList="funsafe_math_optimizations fno_guess_branch_probability fno_ivopts fno_tree_loop_optimize" coToBeReplaced=%%CompilerOption%% TemplateFile=r.scr.template OutputFile=r.scr Test=1 if test "$Test" = 1 then TemplateFile="${TemplateFile}-test" echo "$coToBeReplaced $mhToBeReplaced" >"$TemplateFile" make () { echo -n ">>> make: " cat "$OutputFile" } fi for mica_header in $micaHeaderList do for compiler_option in $compilerOptionList do echo "Calculating $compiler_option correlation for $mica_header" sed "s/$mhToBeReplaced/$mica_header/g;s/$coToBeReplaced/$compiler_option/g" "$TemplateFile" >"$OutputFile" make done done 文件。
  2. 检查r.src脚本是否真正使用make中的替换参数。