Skip to content

Instantly share code, notes, and snippets.

@grepwood
Last active August 30, 2022 16:18
Show Gist options
  • Select an option

  • Save grepwood/2696d40059c21873e8ee to your computer and use it in GitHub Desktop.

Select an option

Save grepwood/2696d40059c21873e8ee to your computer and use it in GitHub Desktop.

Revisions

  1. Michael Dec revised this gist Jan 27, 2015. 1 changed file with 7 additions and 9 deletions.
    16 changes: 7 additions & 9 deletions square.sh
    Original file line number Diff line number Diff line change
    @@ -14,17 +14,15 @@ function fill_middle {
    done
    }

    function draw_top {
    printf "$CORNER_SIGN"
    fill_middle "$SIDE_SIGN"
    echo "$CORNER_SIGN"
    function internal_draw {
    printf "$1"
    fill_middle "$2"
    echo "$1"
    }

    function draw_middle {
    for((X=2; X < SIDE_LENGTH; X++)); do
    printf "$SIDE_SIGN"
    fill_middle " "
    echo "$SIDE_SIGN"
    internal_draw "$SIDE_SIGN" " "
    done
    }

    @@ -44,6 +42,6 @@ elif [ "$SIDE_LENGTH" -eq "1" ]; then
    exit
    fi

    draw_top
    internal_draw "$CORNER_SIGN" "$SIDE_SIGN"
    draw_middle
    draw_top
    internal_draw "$CORNER_SIGN" "$SIDE_SIGN"
  2. Michael Dec created this gist Jan 27, 2015.
    49 changes: 49 additions & 0 deletions square.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    #!/bin/bash
    function internal_rtfm {
    echo "Draw squares in bash, with different sides and corners"
    echo "Usage: $0 x y z"
    echo " x - size of the square to draw"
    echo " y - character for sides"
    echo " z - character for corners"
    exit
    }

    function fill_middle {
    for((Y=2; Y < SIDE_LENGTH; Y++)); do
    printf "$1"
    done
    }

    function draw_top {
    printf "$CORNER_SIGN"
    fill_middle "$SIDE_SIGN"
    echo "$CORNER_SIGN"
    }

    function draw_middle {
    for((X=2; X < SIDE_LENGTH; X++)); do
    printf "$SIDE_SIGN"
    fill_middle " "
    echo "$SIDE_SIGN"
    done
    }

    if [ "$#" -lt "3" ]; then
    internal_rtfm
    fi

    SIDE_LENGTH=$1
    SIDE_SIGN=$2
    CORNER_SIGN=$3

    if [ "$SIDE_LENGTH" -le "0" ]; then
    echo "Side length must be a natural number"
    internal_rtfm
    elif [ "$SIDE_LENGTH" -eq "1" ]; then
    echo $CORNER_SIGN
    exit
    fi

    draw_top
    draw_middle
    draw_top