Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tricky bash to try to run program with different params

Tags:

bash

I want to run a program multiple times with different parameters, and then put the results piped into files that use parameters in their names. Here is what I've come up with:

#!/bin/bash
for i in 'seq 1 5';
do
    for j in 'seq 1 8';
    do
        for m in 'seq 1 8';
        do
            ./program -s i -v j -k m ../input_files/input_file1.txt < results_ijm.txt
         done
     done
done

This doesn't work. It says "no file results_ijm.txt".... I know that - I want it to create this file implicitly.

Otherwise, I also doubt it will assign ijm in the filename correctly - how does it know whether I want the VARIABLES ijm.... or just the characters? It's ambiguous.

like image 366
PinkElephantsOnParade Avatar asked Jun 26 '26 19:06

PinkElephantsOnParade


2 Answers

  • You must use variable $i, $j, $m etc.
  • Better to use ((...)) construct in BASH.

In BASH you can do:

#!/bin/bash
for ((i=1; i<=5; i++)); do
    for ((j=1; h<=8; j++)); do
        for ((m=1; m<=8; m++)); do
            ./program -s $i -v $j -k $m ../input_files/input_file1.txt > "results_${i}${j}${m}.txt"
         done
     done
done
like image 51
anubhava Avatar answered Jun 28 '26 09:06

anubhava


Two problems. As I mentioned in the comments, your arrow is backwards. We want the results of the program to go from stdout to the file so flip that thing around. Second, variables when used gain a dollar sign in front of them... so it won't be ambiguous.

Edited to add: Third thing, use backticks instead of single quotes for seq 1 5 You want the results of that command, not the text "seq 1 5". Thanks @PSkocik

#!/bin/bash
for i in `seq 1 5`;
do
    for j in `seq 1 8`;
    do
        for m in `seq 1 8`;
        do
            ./program -s $i -v $j -k $m ../input_files/input_file1.txt > results_${i}${j}${m}.txt
         done
     done
done
like image 39
JNevill Avatar answered Jun 28 '26 08:06

JNevill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!