練習 - 流量控制

已完成

在此單元中,您將使用畫面右側的 Azure Cloud Shell 作為您的 Linux 終端機。 Azure Cloud Shell 是可透過 Azure 入口網站或在 https://shell.azure.com 存取的 Shell。 您不需要在您的電腦上安裝任何東西,即可開始使用。

當您撰寫指令碼時,只要您輸入合理的值,就可能會如預期運作。 但是,如果著時間流逝,或其他人執行指令碼,則可能會有人輸入非預期的值,或不符合某些其他前置條件。 若要避免此類型的情況,您應處理輸入結果,也就是說,應該將邏輯新增至指令碼,以確保其會在發生問題時提早結束,而且只有在一切都沒問題時,才會繼續執行。

注意

「只」有在未完成此課程模組中的任何先前練習時,才會執行下列命令。 我們假設您已完成先前的練習。 如果您尚未這麼做,您需要幾個檔案。

  • 如果您尚未在此課程模組中完成先前的練習,請在終端機中執行下列的 Bash 命令:

    mkdir webapp
    cd webapp
    touch index.html app.js
    cd ..
    

    這些命令會建立一個目錄,其中包含通常與網頁程式開發相關聯的檔案。

將檢查新增至您的指令碼參數

目前為止您已使用備份指令碼,且已針對此項目新增參數。 您可以藉由新增檢查來確保指令碼只會在提供合理的參數輸入時繼續進行,藉此讓您的指令碼更為安全。

讓我們看看目前的指令碼。 如果您已完成上一個練習,您應該會有一個稱為「Backup.ps1」的檔案。 如果沒有,請在程式碼編輯器中建立檔案並加以開啟:

touch Backup.ps1
code Backup.ps1

將此程式碼新增至該檔案:

Param(
  [string]$Path = './app',
  [string]$DestinationPath = './'
)
$date = Get-Date -format "yyyy-MM-dd"
Compress-Archive -Path $Path -CompressionLevel 'Fastest' -DestinationPath "$($DestinationPath + 'backup-' + $date)"
Write-Host "Created backup at $($DestinationPath + 'backup-' + $date + '.zip')"

如您所知,如果 $Path 指向不存在的目錄,指令碼將會停止回應。

  1. 如果您有一個執行中的 PowerShell shell,請使用現有的 PowerShell shell。 否則,請在終端機中輸入 pwsh 來開始:

    pwsh
    
  2. 透過在 Param 區段之後新增此程式碼,來新增 $Path 參數的檢查,並儲存此檔案:

    If (-Not (Test-Path $Path)) 
    {
      Throw "The source directory $Path does not exist, please specify an existing directory"
    }
    

    您已新增測試來檢查是否 $Path 存在。 如果不是,您可以停止指令碼。 您也會向使用者說明發生錯誤的原因,讓他們可以修正問題。

  3. 執行指令碼以確定指令碼的運作正常:

    ./Backup.ps1 -Path './app'
    

    您應該會看見下列輸出:

    Throw "The source directory $Path does not exist, please specify  …
      |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      | The source directory ./app does not exist, please specify an
      | existing directory
    
  4. 測試指令碼仍能如預期運作。 (請務必先移除上一個練習中的任何備份檔案,再繼續進行操作。)

    ./Backup.ps1 -Path './webapp'
    

    您應該會看到與此相似的訊息:

    Created backup at ./backup-2021-01-19.zip 
    

    如果您再次執行指令碼,其將會停止回應。 其會通知您 ZIP 檔案已存在。 讓我們修正該問題。 我們將新增程式碼,以確保只有當天沒有其他備份 ZIP 檔案存在時,才會建立備份。

  5. 使用該程式碼取代檔案中的程式碼,並儲存此檔案:

    Param(
      [string]$Path = './app',
      [string]$DestinationPath = './'
    )
    If (-Not (Test-Path $Path)) 
    {
      Throw "The source directory $Path does not exist, please specify an existing directory"
    }
    $date = Get-Date -format "yyyy-MM-dd"
    $DestinationFile = "$($DestinationPath + 'backup-' + $date + '.zip')"
    If (-Not (Test-Path $DestinationFile)) 
    {
      Compress-Archive -Path $Path -CompressionLevel 'Fastest' -DestinationPath "$($DestinationPath + 'backup-' + $date)"
      Write-Host "Created backup at $($DestinationPath + 'backup-' + $date + '.zip')"
    } Else {
      Write-Error "Today's backup already exists"
    }
    

    您在這裡做了兩件事。 首先,您已建立新的變數 $DestinationFile。 此變數可讓您輕鬆檢查路徑是否已存在。 其次,您已新增邏輯,指出「只有在檔案不存在時才建立 ZIP 檔案。」此程式碼會執行下列邏輯:

    If (-Not (Test-Path $DestinationFile)) 
    {
      Compress-Archive -Path $Path -CompressionLevel 'Fastest' -DestinationPath "$($DestinationPath + 'backup-' + $date)"
      Write-Host "Created backup at $($DestinationPath + 'backup-' + $date + '.zip')"
    } Else {
      Write-Error "Today's backup already exists"
    }
    
  6. 執行程式碼,以確定指令碼不會停止回應,並套用您的邏輯:

    ./Backup.ps1 -Path './webapp' 
    

    您應該會看見下列輸出:

    Write-Error: Today's backup already exists
    

恭喜! 您已讓指令碼變得更安全。 (請注意,您仍然可以提供像是有問題的輸入 $DestinationPath) 此練習的重點是要說明如何新增檢查。 視指令碼將在其中執行的環境而定,建議您需要執行更少或更多的檢查。 您甚至可能需要進行撰寫測試,檢查全取決於內容。