在bash脚本中保持输出框

时间:2015-05-22 23:05:35

标签: bash output border

我正在研究一个bash脚本,并使它看起来不错。我希望所有输出的命令都在一个盒子里。此框在启动时自动调整大小。 我做了一个drawBox函数 代码:

drawBox ()
{
if [ "$1" ] && [ "$2" ] && [ "$3" ] && [ "$4" ]; then
    printilly=$4
    printillx=$3
    tput cup $1 $2
    num=0
    while [ "$num" -lt "$4" ]; do
        printf "$boxcar" ## The boxcar is the character used for
        num=`expr $num + 1` ##printing the box
    done
    num=1
    sth=`expr $1 + 1 - 1`
    while [ "$num" -lt "$3" ]; do
        tput cup $sth $2
        printf "$boxcar"
        num=`expr $num + 1`
        sth=`expr $sth + 1`
    done
    num=1
    sth=`expr $1 + 1 - 1`
    wid=`expr $4 + $2 - 1`
    while [ "$num" -lt "$3" ]; do
        tput cup $sth $wid
        printf "$boxcar"
        num=`expr $num + 1`
        sth=`expr $sth + 1`
    done
    num=0
    hei=`expr $1 + $3 - 1`
    tput cup $hei $2
    while [ "$num" -lt "$4" ]; do
        printf "$boxcar"
        num=`expr $num + 1`
    done

else
    if [ "$1" == "-help" ]; then
        echo "-help displays this screen"
        echo 'Usage: "drawBox start-height start-width height width"'
        echo "Made by NAME REDACTED May, 20, 2015"
    else
        echo 'Cannot draw box! Storing error info to log.txt'
        echo "[`date`]Invalid Syntax! Please use drawBox start-height start-width height width" > log.txt
    fi
fi
}

这是绘制框的代码。我只需要弄清楚如何实际使输出保持在盒子里,而不打印盒子字符。基本上我需要将代码保留在边框内。喜欢这个

█████████████████████████████████
█Title Here                     █
█████████████████████████████████
█Code output here               █
█without covering the box       █
█but staying inside the box     █
█████████████████████████████████

这是否可以在每次命令后不使用tput cup $ y $ x?我不喜欢那种方法,因为有时候一个命令可以占用多行,通过在盒子上打印使得tput杯感染。这可能吗?此外,函数语法是 $ drawBox start-height开始宽度高度宽度 开始高度是盒子开始的高度 开始宽度是盒子开始的宽度 箱子的高度有多高。 宽度盒子的长度。 (编辑:请使用“boxcar = [character]”设置boxcar来定义要使用的字符。如果没有定义,框可能无法正常工作,或者可能打印为“0”填写没有方括号的[字符],对于你想要盒子字符)

1 个答案:

答案 0 :(得分:0)

如果您的终端具有此功能

,那将很容易
       set_bottom_margin_parm        smgbp
       set_left_margin_parm          smglp
       set_right_margin_parm         smgrp
       set_top_margin_parm           smgtp

       set_lr_margin                 smglr
       set_tb_margin                 smgtb

       set_window                    wind

(见Defined Capabilities)。不幸的是,很多终端都没有。如果是你的情况,你可以调查dialog(1): dialog boxes from shell scripts - 也许这可以满足你的需求。