jeudi 14 novembre 2019

How to repeat a command if file size is zero?

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

Aucun commentaire:

Enregistrer un commentaire