让回声"自动填充"一定宽度的线 [英] Make echo "autofill" a line of certain width
问题描述
我有一个相当大的bash脚本其内容在一些变量,然后相呼应它们。我没有办法事先知道这些变量的长度,我不希望他们缩短或以任何方式进行格式化 - 我只是希望他们在他们读相同的形式呼应
I have a rather large bash script which reads in a number of variables and then echoes them. I have no way to know beforehand the length of these variables and I do not want to shorten them or format them in any way - I just want them echoed in the same form they are read in.
要提高我的脚本的输出可读性我呼应了一些线来帮助眼睛,是这样的:
To improve readability of my script's output I am echoing some lines to help the eye, something like this:
echo "--------------------------------"
echo "Here come some variables |"
echo " |"
echo "a = $a and b = $b"
echo " |"
echo "End of the script |"
echo "--------------------------------"
这将输出:
--------------------------------
Here come some variables |
|
a = 1.23e-19 and b = hello
|
End of the script |
--------------------------------
我想实现的是对回声
(或替代解决方案)来识别它需要多少空车位插入后的变量打印,因此它可以对齐垂直线 |
与他们的休息,所以这看起来就像这样:
What I want to achieve is for echo
(or some alternative solution) to recognize how many empty spaces it needs to insert after the variables are printed so it can align a vertical line |
with the rest of them, so that this output looks like this:
--------------------------------
Here come some variables |
|
a = 1.23e-19 and b = hello |
|
End of the script |
--------------------------------
它并不需要是一个回声
唯一的解决办法,但我会AP preciate简单。任何想法?
It does not need to be an echo
only solution, but I would appreciate simplicity. Any ideas?
推荐答案
因此,使用详细阐述阿克塞尔的回答有点的printf
可以做到这一点。如果线宽然后线宽:
So elaborating a bit on Axel's answer using printf
can do this. If the line width is linewidth then:
a="1.23e-19"
b="hello"
linewidth=30
echo "--------------------------------"
echo "Here come some variables |"
echo " |"
printf "%-${linewidth}s |\n" "a = $a and b = $b"
echo " |"
echo "End of the script |"
echo "--------------------------------"
不正是我想要的。输出:
does exactly what I want. Output:
--------------------------------
Here come some variables |
|
a = 1.23e-19 and b = hello |
|
End of the script |
--------------------------------
这篇关于让回声"自动填充"一定宽度的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!