# Bash



## fullauto (Jan 7, 2013)

Writing a bash script that will draw an ASCII box around user input.  But, I have run into a problem I have not been able to find the solution for.


```
#!/usr/local/bin/bash

###############################################################################
#
# Title:        Box
# Purpose:      Parse and draw ASCII box around text
# Written by:   Tim.Falardeau@SpreadSpectrum.net
#
##############################################################################

VMAX=10
HR="-"
VR="|"

STRING="$@"
echo "STRING = $STRING"

c=1
while [ $c -le $VMAX ]
do
        LINE[$c]=`echo $STRING | cut -d ":" -f $c`
        if [ -z "${LINE[$c]}" ]; then
                break
        fi
        echo ${LINE[$c]}
        c=$((c + 1))
done

NOL=$(( ${#LINE[@]} - 1 ))
echo "\$NOL = $NOL"

BOXH=$(( $NOL + 3 ))
echo "\$BOXH = $BOXH"

LEN=0
for ((c=1; c<=$NOL; c++))
do
        if [ ${#LINE[$c]} -gt $LEN ]; then
                LEN=${#LINE[$c]}
                echo "\$LEN is now $LEN"

        fi
done

BWID=$(( $LEN + 4 ))
echo "\$BWID = $BWID"


PL1=1
PL2=$(( $BOXH - 1 ))
echo "PL1 = $PL1"
echo "PL2 = $PL2"

c=0
while [ $c -le $BOXH ]
do
        if [ $c -eq 0 -o $c -eq $BOXH ]; then
                x=0
                HR="-"
                while [ $x -le $BWID ]
                do
                        HR="$HR-"
                        x=$(( $x + 1 ))
                done
                OUTSTR[$c]=$HR

        elif [ $c -eq $PL1 -o $c -eq $PL2 ]; then
                x=0
                SP=" "
                while [ $x -le $LEN ]
                        do
                                set SP="$SP "
                                x=$(( $x + 1 ))
                        done
                OUTSTR[$c]=$( echo -n "| "$SP" |" )
        fi

echo "\${OUTSTR[$c]} = ${OUTSTR[$c]}"
c=$(( $c + 1 ))
done


exit 0
```

Above code is supposed to assign to top and bottom lines.
as well as the second and second to last line which should look like this.

```
-------------------
|                  |
<LINES WILL GO HERE>
|                  |
--------------------
```

But what I get is

```
--------------------
|  |
<LINES WILL GO HERE>
|  |
--------------------
```

How on earth do I build a line consisting of spaces?
Help me Obi Wan.
You are my only hope


----------



## wblock@ (Jan 7, 2013)

The loop that adds spaces is likely to be slow.  The shell, being dimwitted about spaces, may need special escapes or quoting there to keep from interpreting multiple spaces as one delimiter.

It can probably be done without a loop by using the number of spaces as a parameter to printf(1).  But the quoting will still need to be addressed.


----------



## fonz (Jan 7, 2013)

What's more, if the input text is longer than the display is wide, there's line breaking to be considered as well. I'd be inclined to use C rather than a shell script. And depending on what exactly it is that you're trying to do, perhaps dialog(1) might be worth having a look at.

Fonz


----------



## fullauto (Jan 8, 2013)

I was doing it more just for the practice. I haven't touched computers in years and decided to have a go at it, so this was good practice for me.
I got it to work.  It reads and displays up to 9 lines and self adjusts for size.
aber Danke fur die hilfe!


----------



## wblock@ (Jan 8, 2013)

Please show how you solved it for the benefit of others.


----------



## fullauto (Jan 8, 2013)

```
VMAX=10

STRING="$@"
#echo "STRING = $STRING"
if [ -z "$STRING" ]; then
        echo "Usage: box string:another string:upto and including 9"
        exit 0
fi

c=0
while [ $c -lt $VMAX ]
do
        LINE[$c]=`echo $STRING | cut -d ":" -f "$(( $c + 1 ))"`
        if [ -z "${LINE[$c]}" ]; then
                break
        fi
#       echo "\${LINE[$c]} = ${LINE[$c]}"
        c=$(( c + 1 ))
done

NOL=$(( ${#LINE[@]} - 1 ))
#echo "\$NOL = $NOL"

BOXH=$(( $NOL + 3 ))
#echo "\$BOXH = $BOXH"

LONG=0
for ((c=0; c<$NOL; c++))
do
        if [ ${#LINE[$c]} -gt $LONG ]; then
                LONG=${#LINE[$c]}
        fi
        LEN[$c]=${#LINE[$c]}
#       echo "\${LEN[$c]} is now ${LEN[$c]}"

done

#echo "\$LONG = $LONG"
BWID=$(( $LONG + 2 ))
#echo "\$BWID = $BWID"
#echo "==============================="
PL1=1
PL2=$(( $BOXH -1 ))
#echo "PL1 = $PL1"
#echo "PL2 = $PL2"


c=0
while [ $c -le $BOXH ]
do
        BEG="| "
        END=" |"

        if [ $c -eq 0 -o $c -eq $BOXH ]; then
                x=0
                HR="-"
                while [ $x -le $BWID ]
                do
                        HR="$HR-"
                        x=$(( $x + 1 ))
                done
                OUTSTR[$c]=$HR

        elif [ $c -eq $PL1 -o $c -eq $PL2 ]; then
                x=0
                SPACE=""
                while [ $x -le $(( $BWID - 3 )) ]
                do
                        SPACE="$SPACE "
                        x=$(( $x + 1 ))
                done
                OUTSTR[$c]="$BEG$SPACE$END"
        else
                x=$(( $c - 2 ))
#               echo "\${LEN[$x]} = ${LEN[$x]}"
#               echo "\$LONG = $LONG"
#               echo "\$c = $c"
#               echo "\$x = $x"
                if [ "${LEN[$x]}" -lt "$LONG" ]; then
                        DIFF=$(( $LONG-${LEN[$x]} ))
#                       echo "\$DIFF = $DIFF"
                        i=0
                        while [ $i -lt $DIFF ]
                        do
                                END=" $END"
                                i=$(( $i + 1 ))
                        done
                        OUTSTR[$c]="$BEG${LINE[$x]}$END"
                else
                        OUTSTR[$c]="$BEG${LINE[$x]}$END"
                fi
        fi
c=$(( $c + 1 ))
done

c=0
while [ $c -le $BOXH ]
do
        echo "${OUTSTR[$c]}"
        c=$(( $c + 1 ))
done

exit 0
```

I used a while Loop to echo spaces to a variable.  I just realized my error trying to use printf, So I will probably use that from now on.


----------

