Poop Sheet

Loops

#!/usr/bin/bash

# shellcheck disable=SC2317

ExampleGroup 'loops'

  Example 'while Evaluate and iterate'
    function loop1() {
      NUM=5
      while (( NUM > 0 ))
      do
        printf "Value of NUM = %s \n" "$NUM"
        ((--NUM))
      done
      return 0
    }
    When call loop1
    The output line 1 should match pattern "Value of NUM = 5 "
    The output line 2 should match pattern "Value of NUM = 4 "
    The output line 3 should match pattern "Value of NUM = 3 " 
    The output line 4 should match pattern "Value of NUM = 2 "
    The output line 5 should match pattern "Value of NUM = 1 "
  End

  Example 'until Evaluate and iterate'
    function loop1() {
      NUM=5
      until (( NUM == 0 ))
      do
        printf "Value of NUM = %s \n" "$NUM"
        ((--NUM))
      done
      return 0
    }
    When call loop1
    The output line 1 should match pattern "Value of NUM = 5 "
    The output line 2 should match pattern "Value of NUM = 4 "
    The output line 3 should match pattern "Value of NUM = 3 " 
    The output line 4 should match pattern "Value of NUM = 2 "
    The output line 5 should match pattern "Value of NUM = 1 "
  End

  Example 'As above with for and range'
    loop1() {
      for NUM in {5..1}
      do
        printf "Value of NUM = %s \n" "$NUM"
      done
      return 0
    }
    When call loop1
    The output line 1 should match pattern "Value of NUM = 5 "
    The output line 2 should match pattern "Value of NUM = 4 "
    The output line 3 should match pattern "Value of NUM = 3 " 
    The output line 4 should match pattern "Value of NUM = 2 "
    The output line 5 should match pattern "Value of NUM = 1 "
  End

  Example 'Internal field separator (IFS) default'
    loop1() {
      var1="foo:bar foo bar"
      for val in $var1
      do
        printf '%s\n' "$val"
      done 
    }
    When call loop1
    The output line 1 should match pattern "foo:bar"
    The output line 2 should match pattern "foo"
    The output line 3 should match pattern "bar"
  End

  Example 'Internal field separator (IFS) set to colon'
    loop1() {
      var1="foo:bar foo bar"
      IFS=':'
      for val in $var1
      do
        printf '%s\n' "$val"
      done 
    }
    When call loop1
    The output line 1 should match pattern "foo"
    The output line 2 should match pattern "bar foo bar"
  End

  Example 'Read file using while loop'
    loop1() {
while read -r line
do
  echo "$line"
done <<< "14:00,Brighton,Leicester
14:00,West Ham,Man Utd
16:30,Spurs,Chelsea"
    }
    When call loop1
    The output line 1 should match pattern "14:00,Brighton,Leicester"
    The output line 2 should match pattern "14:00,West Ham,Man Utd"
    The output line 3 should match pattern "16:30,Spurs,Chelsea"
  End

  Example 'Read CSV file, allocating column names, using while loop'
    loop1() {
while IFS="," read -r time team1 team2
do
  echo "$team1 is playing against $team2 at $time"
done <<< "14:00,Brighton,Leicester
14:00,West Ham,Man Utd
16:30,Spurs,Chelsea"
    }
    When call loop1
    The output line 1 should match pattern "Brighton is playing against Leicester at 14:00"
    The output line 2 should match pattern "West Ham is playing against Man Utd at 14:00"
    The output line 3 should match pattern "Spurs is playing against Chelsea at 16:30"
  End

End