如何在固定宽度的字符串中居中放置一行文本?

How to center a line of text in a fixed width string?

我有一个用于我所有服务器的标准横幅,我通常手动输入它们,但随着我的服务器数量不断增加,是时候将其自动化了。

通常,我的横幅应该是这样的:

*****************************************************************
*       .:Welcome to hostname.internal.mynet.net:.              *
*                                                               *
* This is a private server maintained by and exclusively for    *
* use by me. No authorization is given or granted to any party  *
* unless explicit permission is given. Attempts to circumvent,  *
* disable, or otherwise interfere with normal operation will be * 
* prosecuted to the fullest extent of applicable laws.          *
*                                                               *
*              UNAUTHORIZED ACCESS PROHIBITED                   *
***************************************************************** 

这是想要的效果,但是我的主机名有不同的字符,所以有时格式不正确,例如:

*****************************************************************
*       .:Welcome to somelongrandomhostname.internal.mynet.net:.     *
*                                                               *
* This is a private server maintained by and exclusively for    *
* use by me. No authorization is given or granted to any party  *
* unless explicit permission is given. Attempts to circumvent,  *
* disable, or otherwise interfere with normal operation will be * 
* prosecuted to the fullest extent of applicable laws.          *
*                                                               *
*              UNAUTHORIZED ACCESS PROHIBITED                   *
***************************************************************** 

如果主机名更短:

*****************************************************************
*       .:Welcome to abc.internal.mynet.net:.        *
*                                                               *
* This is a private server maintained by and exclusively for    *
* use by me. No authorization is given or granted to any party  *
* unless explicit permission is given. Attempts to circumvent,  *
* disable, or otherwise interfere with normal operation will be * 
* prosecuted to the fullest extent of applicable laws.          *
*                                                               *
*              UNAUTHORIZED ACCESS PROHIBITED                   *
***************************************************************** 

我在脚本中使用的相关代码是:

# banner
echo "
*****************************************************************
*       .:Welcome to $hostname.internal.mynet.net:.             *
*                                                               *
* This is a private server maintained by and exclusively for    *
* use by me. No authorization is given or granted to any party  *
* unless explicit permission is given. Attempts to circumvent,  *
* disable, or otherwise interfere with normal operation will be *
* prosecuted to the fullest extent of applicable laws.          *
*                                                               *
*              UNAUTHORIZED ACCESS PROHIBITED                   *
***************************************************************** " > banner

我不确定如何或使用什么作为最好的工具来生成完全按照第一个示例中提供的横幅,而不管主机名的长度如何?寻找思路,"frame"(如星号字符所示)应该是一个固定值。

您可以尝试使用此代码段中的参数:

#!/bin/sh

for h in io sun moon earth quaoar neptune ganymede alphaCentauri; do
  pad=$(printf '%*s.:Welcome to %s.internal.mynet.net:.\n' \
       -$(expr 20 - ${#h} / 2) " " "$h")
  printf '* %-75s *\n' "$pad"
done

输出类似

*                    .:Welcome to io.internal.mynet.net:.                     *
*                    .:Welcome to sun.internal.mynet.net:.                    *
*                   .:Welcome to moon.internal.mynet.net:.                    *
*                   .:Welcome to earth.internal.mynet.net:.                   *
*                  .:Welcome to quaoar.internal.mynet.net:.                   *
*                  .:Welcome to neptune.internal.mynet.net:.                  *
*                 .:Welcome to ganymede.internal.mynet.net:.                  *
*               .:Welcome to alphaCentauri.internal.mynet.net:.               *

它使用 ${#h} 来获取 $h 中的字符数,将其除以二,然后使用 20 减去空格数向左填充。 请注意使用 %*s printf 格式说明符和 negative 参数来指示 左对齐 放置。然后将结果用于固定宽度的 75 个字符的字符串。