Home > Blockchain >  Create multiple .sh files using a loop
Create multiple .sh files using a loop

Time:11-08

I want to prepare several .sh files to run some simulations.

Let's say that the command that I want to run multiple times is:

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

where

  • the_script is the script I want to run
  • -m is the file to be used to run the simulation
  • -r the file to save the results

I have a lists of mutants, so essentially I want to create loop that will give me 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

and so on in one .sh file.

However, as I have 100 mutants I want to create a separate .sh file for each 5 mutants.

The expected output would be

run_sim_1.sh which contains (omitting the for nodes in $node_list; do / done for clarity)

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 which contains

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"

CodePudding user response:

You can use this script:

#!/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
  • I named the nodes 1,2,3,4,... just to show you that they are grouped 5 per .sh file, but they could have any name. I do not take for granted that they follow a numeric sequence. It will work with anything.
  • variable counter: counts how many nodes have been processed in the for loop.
  • variable shcounter: counts the number of .sh files that have been created.
  • each time a node is processed, the command with that node is written to the current .sh file (current is designated by the shcounter). Also the counter is incremented. If it is greater than 5, it is reset to 0, and the shcounter is incremented.

Running this script creates 3 files. run_sim1.sh (nodes 1->5), runsim2.sh (nodes 6->10), runsim3.sh (node 11). I wanted something not divisible by 5 to show my algorithm could handle it.

  • Related