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.
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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment