如果文件大小为零,如何重复命令?

我有一个脚本可以复制并粘贴其中包含一些文件的文件夹,并更改其中一个文件的某些值。事实是,导出的文件夹中的该文件随机显示为空白。因此,我想在脚本中添加一个检查点,以检查新文件大小是否大于0,如果不是,请再次重复该过程,直到文件大小大于0。 这是我到目前为止所得到的,但似乎无法正常工作:

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,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
jerrytoto 回答:如果文件大小为零,如何重复命令?

嗯...统计信息使用情况过于复杂。

为什么不使用以下内容:

Private Function GetInstalledVersion() As Single
    Dim version As Single = Nothing
    Using root = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,RegistryView.Registry64)
        Using key = root.OpenSubKey("Software\7-Zip",False)
            Dim appPath As String = CStr(key.GetValue("Path"))
            Dim appVersion = FileVersionInfo.GetVersionInfo(Path.Combine(appPath,"7z.exe"))
            If Not Single.TryParse(appVersion.ProductVersion,version) Then
                Console.WriteLine("Unable to retrieve installed version")
            End If
            Return version
        End Using
    End Using
End Function

Private Function GetWebVersion() As Single
    Dim r As Regex = New Regex("(?<=Download 7-Zip ).*?(?= )")
    Dim web = New Net.WebClient().DownloadString("https://www.7-zip.org/download.html")
    Dim version As Single = Nothing
    If Not Single.TryParse(r.Match(web).Value,version) Then
        Console.WriteLine("Unable to retrieve web version")
    End If
    Return version
End Function

while [ ! -s "$file" ]; do your_stuff done 转换为“如果不是(文件大小不为零或不存在)”。

,

您在此行遇到问题:

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

您同时读写一个文件。不推荐使用。 使用缓冲文件。

本文链接:https://www.f2er.com/3101503.html

大家都在问