Gianmarco Broilo
Gianmarco Broilo

Reputation: 961

Mathematical operations using floats in bash

I have a script that increments a variable in a text file using sed by 0.25 (from 0.25 to 1) in a while loop and saves the new file with the extension of the increment. Everything looks fine but the script does not change the variable inside of the text file but it properly export them with their increment


read -p "define the increment " length_user
read -p "define the minimum length" length_start_user
length_increment=0
length_define=$length_user

length_start=$length_start_user
while [ $length_increment -lt 4 ]; do
  cp /home/students/gbroilo/Desktop/Prova.py /home/students/gbroilo/Desktop/Prova_$length_start.py

  sed -i 's/l=l0/l='$length_start'/' Prova_$length_start.py;
  length_start=$(echo "$length_start + $length_define" |bc)

  length_increment=$(( $length_increment + 1 ))
done

Upvotes: 0

Views: 47

Answers (1)

chepner
chepner

Reputation: 531075

Use seq:

cd /home/students/gbroilo/Desktop/
for length in $(seq $length_start_user $length_define 4); do
  sed "s/l=10/l=$length/" Prova.py  > Prova_$length.py
done

That said, don't modify the Python script like this. Instead, change it to take the length as a command line argument, so that you can run

python Prova.py 3.6

instead of

python Prova_3.6.py

That would be a single call to sed, something like:

sed -i '1i\
import sys
s/l=10/l = sys.argv[0]' Prova.py

though there's no real reason to automate this; just open the file in your favorite editor and make the change manually.

Upvotes: 1

Related Questions