【问题标题】:Keep output in box in bash script将输出保存在 bash 脚本的框中
【发布时间】:2015-05-22 23:05:34
【问题描述】:

我正在编写一个 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 cup 具有感染性。这可能吗?此外,函数语法是 $ drawBox start-height start-width 高度 宽度 开始高度是盒子开始的高度 开始宽度是盒子开始的宽度 height 盒子的高度。 width 盒子有多长。 (编辑:请使用 "boxcar=[character]" 设置 boxcar 来定义要使用的字符。如果不定义,box 可能无法正常工作,或者可能打印为 "0" 不带方括号的 [character] 填写为您的想要盒子字符)

【问题讨论】:

    标签: bash output border


    【解决方案1】:

    如果你的终端有这些功能会很容易

    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

    设置窗口风

    (请参阅Defined Capabilities)。不幸的是,许多终端没有。如果您的情况是这样,您可以查看dialog(1): dialog boxes from shell scripts - 也许这会满足您的需求。

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 2012-04-03
      相关资源
      最近更新 更多