当前位置:  开发笔记 > 编程语言 > 正文

printf bash - 在标记包围的现有行的中间打印文本

如何解决《printfbash-在标记包围的现有行的中间打印文本》经验,为你挑选了1个好方法。

它似乎并不那么简单.至少对于我来说.

我需要在printf文本中有一个变量.它是这样的:

FOO="User data"
+++++++++++++++++++ $FOO +++++++++++++++++++++

会输出

+++++++++++++++++++ User Data +++++++++++++++++++++

FOO="Fooooooo barrrr"
+++++++++++++++++++ $FOO +++++++++++++++++++++

应输出

++++++++++++++++ Fooooooo barrrr ++++++++++++++++++

FOO="Foooooooooooooooooooo barrrrr"
+++++++++++++++++++ $FOO +++++++++++++++++++++

应该

+++++++++ Foooooooooooooooooooo barrrrr +++++++++++

正如你所看到的,我需要一个变量位于n长度线的中间,由+mark 包围.如何使用printf和其他默认可用命令实现?

(Debian 8)



1> Cyrus..:
declare -i x1 x2 x3 width
foo="User data"
width=50                # total width
x2=${#foo}+2            # length of $foo and 2 whitespaces
x1=(50-x2)/2            # length of first part
x3=$width-x1-x2         # length of last part
for ((i=1;i<=$x1;i++)); do echo -n "+"; done
echo -n " $foo "
for ((i=1;i<=$x3;i++)); do echo -n "+"; done

输出:

+++++++++++++++++++ User data ++++++++++++++++++++

foo="stackoverflow.com":

+++++++++++++++ stackoverflow.com ++++++++++++++++

推荐阅读
乐韵答题
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有