I've been learning Bash and my teacher made an exercise that got me puzzled.
I'm just struggling with adding the spaces in front of the words.
The code should account for the number of characters in each word and adjust the number of asterisks and spacing for it to always be aligned.
This is what it should look like:
***********
* Hello *
* World *
* good *
* etceter *
***********
My code:
#determine biggest word
words=("Hello" "World" "good" "etceter")
numchar=0
for i in ${words[@]}; do
if [ ${#i} -gt $numchar ]
then
numchar=${#i}
fi
done
#write *
a=-4
while [ $a -lt $numchar ]; do
printf "*"
((a++))
done
#write array
echo
for txt in ${words[@]}; do
space=$(($numchar-${#txt}))
s=0
echo "* $txt "
while [ $s -lt $space ]; do
printf " "
((s++))
printf "*"
done
done
#write *
a=-4
while [ $a -lt $numchar ]; do
printf "*"
((a++))
done
I'm struggling with the #write array part.
Many thanks in advance!