如何在条件为真之前重新运行bash脚本的一部分?

时间:2018-01-24 22:43:58

标签: bash loops

我的代码如下:

file=$inputfile
prefix=$prefixPath
while read line
do
    sourcePath=$(echo $line | cut -f1 -d,)
    var1=$(echo $line | cut -f2 -d,)
    var2="$(echo -e "${var1}" | tr -d '[:space:]')"

    destinationPath=$(echo "$prefix$var2")

    echo $sourcePath
    echo $datalakePath

# Upload data from specific path to DataLake.
az dls fs upload --account eanpdlstore2 --source-path $sourcePath  --
destination-path $datalakePath --overwrite
  done < $file

 #Download data from DataLake to local temp dir
 az dls fs download --account eanpdlstore2 --destination-path $temp_path --
 source-path $datalakePath --overwrite

#Tar the whole dir and calculate the sha1sum
tar czf sourceFolder.tar.gz $sourceFolderPath | sha1sum > checksum1.txt
tar czf tempFolder.tar.gz $temp_path | sha2sum > checksum2.txt

 if diff -qr checksum1.txt checksum2.txt != True:
 then
     loop until check is matched.
 else:
     echo 'checksum matched'

我想检查校验和是否匹配,如果不是我想再次循环代码,写它的最佳方法是什么。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

移动逻辑以将下载运行到循环中,因此只要循环执行就会隐式重新运行。

下面缺少原始问题中也缺少的一些部分(例如,temp_path永远不会设置,内容永远不会移动到destinationPath),但它应该提供一个坚实的地方开始。

各种|| continue位确保我们返回until并在失败时重试循环。

#!/usr/bin/env bash
set -o pipefail  # if any part of a pipeline fails, consider the entire command to fail

file=$1          # input file
prefix=$2        # destination prefix

while IFS=, read -r sourcePath var1; do
  var2=${var1//[[:space:]]/}
  destinationPath="$prefix$var2"
  checksum1=
  checksum2=

  until [[ $checksum1 && $checksum2 && "$checksum1" = "$checksum2" ]]; do
    # Upload data from specific path to DataLake.
    az dls fs upload --account eanpdlstore2 --source-path "$sourcePath" --destination-path "$datalakePath" --overwrite || continue

    #Download data from DataLake to local temp dir
    az dls fs download --account eanpdlstore2 --destination-path "$temp_path" --source-path "$datalakePath" --overwrite || continue

    #Tar the whole dir and calculate the sha1sum
    checksum1=$(tar -czf sourceFolder.tar.gz "$sourceFolderPath" | sha1sum) || checksum1=
    checksum2=$(tar -czf tempFolder.tar.gz "$temp_path" | sha1sum) || checksum2=
  done </dev/null ## safety: stop anything here from consuming lines from the input file
done <"$file"