unix 使用循环创建多个.sh文件

h7appiyu  于 2022-11-04  发布在  Unix
关注(0)|答案(1)|浏览(213)

我想准备几个.sh文件来运行一些模拟。
假设我想运行多次的命令是:

node_list = "Positive,Negative"

for nodes in $node_list; do
    python3.7 path_to_script/the_script -m "file_mutation1.csv" -r "results_mutation1.csv"
done

其中

  • _script是要运行的脚本
  • -m是用于运行模拟的文件
  • -r文件以保存结果

我有一个变种列表,所以我想创建一个循环,给予mutant_list = [TP53, NFKB, ABL1]

for nodes in $node_list; do
    python3.7 path_to_script/the_script -m "file_mutation_TP53.csv" -r "results_mutation_TP53.csv"
done

for nodes in $node_list; do
    python3.7 path_to_script/the_script -m "file_mutation_NFKB.csv" -r "results_mutation_NFKB.csv"
done

for nodes in $node_list; do
    python3.7 path_to_script/the_script -m "file_mutation_ABL1.csv" -r "results_mutation_ABL1.csv"
done

等等。
然而,由于我有100个突变体,我想为每5个突变体创建一个单独的.sh文件。
预期输出为
run_sim_1.sh包含(为清楚起见,省略for nodes in $node_list; do / done

python3.7 path_to_script/the_script -m "file_mutation1.csv" -r "results_mutation1.csv"
python3.7 path_to_script/the_script -m "file_mutation2.csv" -r "results_mutation2.csv"
python3.7 path_to_script/the_script -m "file_mutation3.csv" -r "results_mutation3.csv"
python3.7 path_to_script/the_script -m "file_mutation4.csv" -r "results_mutation4.csv"
python3.7 path_to_script/the_script -m "file_mutation5.csv" -r "results_mutation5.csv"

run_sim_2.sh,其中包含

python3.7 path_to_script/the_script -m "file_mutation6.csv" -r "results_mutation6.csv"
python3.7 path_to_script/the_script -m "file_mutation7.csv" -r "results_mutation7.csv"
python3.7 path_to_script/the_script -m "file_mutation8.csv" -r "results_mutation8.csv"
python3.7 path_to_script/the_script -m "file_mutation9.csv" -r "results_mutation9.csv"
python3.7 path_to_script/the_script -m "file_mutation10.csv" -r "results_mutation10.csv"
8qgya5xd

8qgya5xd1#

您可以使用以下脚本:


# !/bin/bash

node_list=( 1 2 3 4 5 6 7 8 9 10 11 )

counter=1
shcounter=1

for node in "${node_list[@]}"
do
    echo "python3.7 path_to_script/the_script -m \"file_mutation$node.sh\" -r \"results_mutation$node.csv\"" >>run_sim_${shcounter}.sh
    if [[ $counter -ge 5 ]]
    then
        (( shcounter = shcounter + 1 ))
        counter=0
    fi
    (( counter = counter + 1 ))
done
  • 我将节点命名为1,2,3,4......只是为了向您显示每个.sh文件中有5个节点,但它们可以有任何名称。我并不认为它们遵循数字序列是理所当然的。它可以用于任何对象。
  • 变量counter:计算for循环中已处理的节点数。
  • 变量shcounter:会计算已建立的.sh档案数目。
  • 每次处理一个节点时,该节点的命令被写入当前的.sh文件(当前由shcounter指定)。2同时,计数器增加。3如果它大于5,它被重置为0,并且shcounter增加。

运行这个脚本会创建3个文件。run_sim1.sh(节点1-〉5),runsim2.sh(节点6-〉10),runsim3.sh(节点11)。我想要一个不能被5整除的文件来显示我的算法可以处理它。

相关问题