0

I have a bash script that edits some values in a python file called prova.py and export it to another directory using a while loop. The problem is that when I try to open the prova.py files, some are python files (text/x-python) and some are plain text documents (text/plain). This happens randomly, as if I run the script multiple times some files are in python and then the next run they are as text.

I tried to substitute ${file} in the last cat with prova.py but this doesn't seem to change the results

#!/bin/bash
path='/home/students/gbroilo/Desktop/Script'
file='prova.py'

step_x=1
while [ $step_x -lt 5 ]; do  #maximum value of x
  step_y=1
  while [ $step_y -lt 4 ]; do   #maximum value of y
    cp -rf ${path}/Template ${path}/Template_${step_x}_${step_y}      #copy folder and rename it with variable step x and y
    cd ${path}/Template_${step_x}_${step_y}                           #change directory and open the Template folder

    x=$( cat ${file} | sed -n '/x=[^0-9]*/p' | sed 's/[^0-9]*//g' )   #isolate the value of the x coordinate
    x=$( expr ${x} + ${step_x} )                                      #define the increment of the x coordinate
    y=$( cat ${file} | sed -n '/y=[^0-9]*/p' | sed 's/[^0-9]*//g' )   #isolate the value of the y coordinate
    y=$( expr ${y} + ${step_y} )                                      #define the increment of the y coordinate

    cat ${file} | sed "s/x=[0-9]*/x=${x}/g" | sed "s/y=[0-9]*/y=${y}/g" > prova.py   #substitute the old value of x and y with their new incremented value
    step_y=$(( ${step_y} + 1 )) #increment by one the current value of y
  done
  step_x=$(( ${step_x} + 1 )) #increment by one the current value of x
done

All the exported prova.py files should be Type: Python script (text/x-python)

Gianmarco Broilo
  • 701
  • 1
  • 6
  • 10

0 Answers0