0

This script should give values to the program through the command line and store the output of the terminal in their corresponding files. Since I cannot put a decimal-point number as part of the label of the file, I intent to multiply each kappa by 10000 to turn them into integers and use them as labels, but I did it wrong in the code and I don't know how to it properly. How does it work? Thank you!

#!/bin/bash

for kappa in $(seq 0.0001 0.000495 0.01);
    do
        kappa_10000 = $kappa * 10000;

        for seed in {1..50};
        do
                ./two_defects $seed $kappa > "equilibration_step_seed${seed}_kappa${kappa_10000}.txt";
        done
    done 
  • First thing that jumps out is line 5, `kappa_10000 = $kappa * 10000`. In Bash, don't put spaces around the equals sign. Also, you're calling the math part wrong. `kappa_10000=$((kappa * 10000))` – Guest Jan 23 '17 at 09:08
  • An interesting and extensive cross-site duplicate [here](http://unix.stackexchange.com/questions/40786/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks). – Aserre Jan 23 '17 at 09:29
  • It is very important what you said about spaces around the equal sign! I noticed this also when playing on the terminal! But you can't do floating-point arithmetic in bash. Thanks Guest –  Jan 23 '17 at 09:47
  • Possible duplicate of [How do I use floating-point arithmetic in bash?](http://stackoverflow.com/questions/12722095/how-do-i-use-floating-point-division-in-bash) – Inian Jan 23 '17 at 09:58
  • Why can't you store with the decimal point? Or replace the point with an underscore? Or use `${kappa:2}` ? – Walter A Jan 23 '17 at 17:03
  • Basically because I started learning bash two days ago and I don't know anything about it yet. That was the first idea I had and I wanted to work it out @WalterA –  Jan 24 '17 at 07:37

1 Answers1

2

Bash does not do floating-number calculation as pointed out by @Inian. A program like bc must be called and its output can be directly stored in a variable as follows:

for kappa in $(seq 0.0001 0.000495 0.01)
do 
    kappa_10000=$(echo "$kappa*10000/1" | bc)
    echo kappa_10000
done

The output in the terminal would be

1
5
10
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100

The /1 operation must be added to make the output an integer.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578