3

I have a Python script using the parser package for setting option flags (like -d dataset and -s size etc. and finally the results are written to a file. How can i run the command many times (sequentially) with different option flags for each run?

I need something like this:

datasets = [a,b,c]
sizes = [100,200,300]

for dataset in dataset:                           #specify parameters
    for size in sizes:                            #specify more parameters
         python script.py -d dataset -s size      #run script

What would be the best (or even 'a') way to do this?

1 Answer 1

6

A direct translation of your pseudo/python-code to bash, with arrays instead of lists and classic for loops:

#!/bin/bash

datasets=('a' 'b' 'c')
sizes=(100 200 300)

for dataset in "${datasets[@]}"; do
    for size in "${sizes[@]}"; do
        python script.py -d "$dataset" -s "$size"
    done
done

If sizes are more like a range, and not a list of hand-picked values, you can use the brace expansion:

for size in {100..300..100}; do
    # ...
done

or the arithmetic for loop:

for ((size=100; size<=300; size+=100)); do
    # ...
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.