-2

I have the following problem:
I want to run from the shell the following:

python test.py -f fname.csv -s 0.0 -c 0.0 > outS0.0C0.0.txt

The issue is that I want s and c to loop over all values from 0.0 to 1.0 for both values and store the results to files of the above format. Also, if it is possible, to move respectively the output files to subfolders, like: in .\S0.0 all out* files with s starting with outS0.0* and so on. So the result will be a subfolder set of respective files.

Thank you!

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Paradigm
  • 149
  • 1
  • 2
  • 10
  • 1
    Saying that you loop over all values from 0.0 to 1.0 leaves some room for interpretation whether that would be 0.0, 0.1, 0.2, etc. or possibly 0.0, 0.01, 0.02, and so on. That aside, what have you tried this far? Have you tried using `for`-statement in Bash? It's commonly expected that you show some effort in terms of actual code when posting to SO. –  Jul 11 '16 at 17:07
  • And what have you tried? Mathematically, "all values" in a range is an infinite set, though the limited precision of standard floating point maths bounds it to something like 2^46 discrete numbers. – tripleee Jul 11 '16 at 17:15
  • And you also cannot use Google...? – tripleee Jul 11 '16 at 17:20
  • Yes, that is what I am doing plus I study Mendel Cooper-Advanced bash-scripting guide (2004) Thank you. – Paradigm Jul 11 '16 at 17:22
  • 1
    The ABS is a very poor choice of references. Consider the Wooledge BashGuide -- at http://mywiki.wooledge.org/BashGuide -- or the bash-hackers wiki. – Charles Duffy Jul 11 '16 at 17:24
  • 1
    As for floating-point math, it's covered in BashFAQ #22: http://mywiki.wooledge.org/BashFAQ/022 – Charles Duffy Jul 11 '16 at 17:25
  • (That said, there are really *two* questions here -- the better one about iterating over floating-point numbers in a range in bash, the other one about splitting your output into subdirectories. I'm ignoring the latter, intentionally, as you haven't followed site guidelines in asking it -- showing what you tried and what difficulty you had in that attempt -- and also inasmuch as its inclusion violates the "one question to a question" rule, making this eligible to be closed as "too broad"). – Charles Duffy Jul 11 '16 at 17:32
  • `seq` is a good tool for this usage - as in `for c in $(seq 0 0.1 1); do ...` – twalberg Jul 11 '16 at 17:40
  • Re floating point math in bash, you can sort of fake things, as [I suggested in another answer](http://stackoverflow.com/a/33759747/1072112). But you'll be better off using tools (assuming you have them) designed for the job, like `seq` or `jot`. Or even calculators like `dc` or `bc`. Hacking bash to do something it's just not good at is a net waste of time. – ghoti Jul 11 '16 at 17:42

3 Answers3

0

Something like this will do, without any tools (such as seq) which aren't specified either by POSIX or bash's documentation:

#!/bin/bash
#      ^^^^- not /bin/sh; bash is required for C-style for loop syntax

for ((s=0; s<=10; s++)); do
  for ((c=0; c<=10; c++)); do
    s_str=$(awk -v i="$s" 'BEGIN { printf "%1.1f\n", i / 10 }')
    c_str=$(awk -v i="$c" 'BEGIN { printf "%1.1f\n", i / 10 }')
    python test.py -f fname.csv -s "$s_str" -c "$c_str" >"outS${s_str}C${c_str}.txt"
  done
done
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

Bash cannot do floating point math. But you can use awk to create your numbers.

#!/bin/sh

counter() {
    awk 'BEGIN { for (i=0.0; i<=1; i+=0.1) { printf("%.1f\n",i) } }'
}

counter | while read s; do
    mkdir -p S${s}
    counter | while read c; do
        python test.py -f fname.csv -s ${s} -c ${c} > outS${s}C${c}.txt
        mv outS${s}C${c}.txt S${s}/
    done
done

This creates directories for each value of s, then runs your python command for each value of s and c, moving each resultant file into a target directory. I'm assuming you have a reason for not redirecting the output into files already in that target directory -- otherwise, it would make more sense to drop the mv and do this:

        python test.py -f fname.csv -s ${s} -c ${c} > S${s}/outS${s}C${c}.txt

If you want other numbers for your counter, it would be easy to add options to this shell function to make it behave like seq or jot.

ghoti
  • 45,319
  • 8
  • 65
  • 104
0
#!/bin/sh
for ((s=0; s<=10; s++)); do
  for ((c=0; c<=10; c++)); do
    s_str=$(awk -v i="$s" 'BEGIN { printf "%1.1f\n", i / 10 }')
    c_str=$(awk -v i="$c" 'BEGIN { printf "%1.1f\n", i / 10 }')
    s_str=$(sed 's/,/./' <<< $s_str)
    c_str=$(sed 's/,/./' <<< $c_str)
    mkdir -p S${s_str}
    echo "Doing case:  S=${s_str},  C=${c_str}"
    python test.py -f fname.csv -s "$s_str" -c "$c_str"  > "outS${s_str}C${c_str}.txt"
    mv outS${s_str}C${c_str}.txt S${s_str}/
  done
done                                                                                                                                                   
Paradigm
  • 149
  • 1
  • 2
  • 10