Skip to content

Instantly share code, notes, and snippets.

@grepwood
Last active August 30, 2022 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grepwood/2696d40059c21873e8ee to your computer and use it in GitHub Desktop.
Save grepwood/2696d40059c21873e8ee to your computer and use it in GitHub Desktop.
Draws squares in Bash
#!/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 internal_draw {
printf "$1"
fill_middle "$2"
echo "$1"
}
function draw_middle {
for((X=2; X < SIDE_LENGTH; X++)); do
internal_draw "$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
internal_draw "$CORNER_SIGN" "$SIDE_SIGN"
draw_middle
internal_draw "$CORNER_SIGN" "$SIDE_SIGN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment