0

I have this script that prints out a box frame with Asterisk signs, and I need to make it so that the script prints out multiple boxes under each other. How can I do it?

#!/bin/bash
#
echo -n "Please enter the amount of lines: "
read lines
echo -n "Please enter the amount of *: "
read sign
for((i = 1; i <= $lines; i++))
do
  echo -n "$i "
  if [ $i -eq 1 -o $i -eq $lines ]; then
  for((j = 1; j <= $sign; j++))
  do
    echo -n "* "
  done

  else
    echo -n "* "
    for((j = 2; j < $sign; j++))
    do
      echo -n "  "
    done
    echo -n "* "
  fi
  echo
done
#end
  • Just run the code in a loop? Also, this question, or a similar one, came up recently. Is it homework? Are you later supposed to add a star in the center of the box like this person wanted? Don't output individual characters in loops, it's horribly inefficient. – Kusalananda May 29 '18 at 6:59
  • pretty much yes, i keep overthinking to get the answer. – MargusL May 29 '18 at 8:53
0

If you just want the number of boxes as an input, it is just:

#!/bin/bash
#
echo -n "Please enter the amount of lines: "
read lines
echo -n "Please enter the amount of *: "
read sign
echo -n "Please enter the amount of boxes: "
read boxes
for ((h = 1; h <= $boxes; h++))
do
    for((i = 1; i <= $lines; i++))
    do
        echo -n "$i "
        if [ $i -eq 1 -o $i -eq $lines ]; then
            for((j = 1; j <= $sign; j++))
            do
                echo -n "* "
            done
        else
            echo -n "* "
            for((j = 2; j < $sign; j++))
            do
                echo -n "  "
            done
            echo -n "* "
        fi
        echo
    done
done
#end

Not sure whether this is what you want though, because if you wrote your code you should have probably figured it out.

  • i keep overthinking and i was curious to find out a simple way – MargusL May 29 '18 at 8:54
  • Thank you for your help, now i understand and possibly do not overthink. – MargusL May 29 '18 at 8:57
  • No problem. I guess this was the simplest way to extend exactly what you were doing in the way you were doing it. Feel free to accept the answer if you think it was what you needed :) – myradio May 29 '18 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.