我有一个文本文件,其中包含一些ASCII艺术。我正在尝试让它在终端启动时打印到终端。我知道要这样做,我在.bashrc文件中添加了cat myfi。我被困住试图使猫的输出居中。我尝试使用tput cols不成功。
任何人都可以给我一些提示,或者如果可以的话。
一种方法是执行以下步骤:
这是一个示例脚本,awk
用于执行计算:
#!/bin/sh
input="$1"
twidth=$(tput cols)
echo terminal width is $twidth
indent=$(awk -v twidth=$twidth ' {
w=length();
if (w > fwidth) fwidth=w;
}
END {
indent=int((twidth-fwidth)/2);
print (indent > 0 ? indent : 0);
}' < "$input")
echo indent is $indent
awk -v indent=$indent '{ printf("%*s", indent, " "); print; }' < "$input"
这是一个对大字母L的测试:
$ cat /tmp/L
#
#
#
#
#
#
#######
$ ./center /tmp/L
terminal width is 81
indent is 37
#
#
#
#
#
#
#######
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句