【发布时间】: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] 填写为您的想要盒子字符)
【问题讨论】: