1

I have a script that copy and paste a folder with some files in it and change some values of one of these files. The thing is that randomly this file in the exported folder comes out blank. So I would like to add a checkpoint in my script that will check if the new file size is > 0 and if not, repeat the process again until the file size is > 0. This is what i got so far but it doesnt seem to work fine:

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 5 ]; 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

    #command to export .med file to the right template directory
    gawk -i inplace 'NR==455{print "try:\n Mesh_1.ExportMED( r\047/'${path}'/'Template_${step_x}_${step_y}'/Mesh_1.med\047,\
0, SMESH.MED_V2_2, 1, None ,1)\n pass\nexcept:\n print \047ExportToMEDX() failed. \
Invalid file name?\047"}1' ${file}



    #check for size of the prova.py file
  filesize=$(stat -c%s "prova.py")
  echo "size of ${prova.py} = $filesize"

  if (( filesize > 0 )); then
    echo "file is correct"
  else
    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

    #command to export .med file to the right template directory
    gawk -i inplace 'NR==455{print "try:\n Mesh_1.ExportMED( r\047/'${path}'/'Template_${step_x}_${step_y}'/Mesh_1.med\047,\
0, SMESH.MED_V2_2, 1, None ,1)\n pass\nexcept:\n print \047ExportToMEDX() failed. \
Invalid file name?\047"}1' ${file}

  fi

    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
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Gianmarco Broilo
  • 701
  • 1
  • 6
  • 10
  • https://stackoverflow.com/a/5920355/469210 – borrible Nov 14 '19 at 15:13
  • Can you reduce your code to the minimum required that still shows the problem you're facing? That includes getting rid of all the sed and awk stuff. Then, you have to describe *what exactly* doesn't work. It's unlikely that somebody looks at all your code (without having your exact setup) and can guess what doesn't work for you. See [mcve]. – Benjamin W. Nov 14 '19 at 15:46
  • 1
    Run your script through http://shellcheck.net first. For starters, it will tell you to stop using `x=$( expr $[x} + ${step_x} )` when you can use `x=$((x + step_x))` instead. – chepner Nov 14 '19 at 15:48
  • `step_x=1 while [ $step_x -lt 5 ]; do #maximum value of x ... step_x=$(( ${step_x} + 1 ))` in bash just do `for ((step_x = 1; step_x < 5; step_x++)); do`. – KamilCuk Nov 15 '19 at 09:36

2 Answers2

2

Hmmm... overcomplicated stat usage.

Why not use something like:

while [ ! -s "$file" ]; do
    your_stuff
done

[ ! -s "$file" ] translates to "if not (file is not zero size or does not exist)".

Orsiris de Jong
  • 2,819
  • 1
  • 26
  • 48
0

You have problem on this line:

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

You read and write one file at same time. Its deprecated. Use buffer file.

Nigredon
  • 81
  • 1
  • 3