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

时间:2016-04-10 13:57:57

标签: shell text-alignment

我有一个标准横幅,我用于所有服务器,我通常手动输入它们,但随着我的服务器群的增长,是时候自动化了。

通常,我的横幅应如下所示:

*****************************************************************
*       .: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

我不知道如何或者使用什么作为生成横幅的最佳工具,与第一个示例中提供的一样,无论主机名的长度如何?寻找想法,“框架”(由星号字符表示)应该是固定值。

1 个答案:

答案 0 :(得分:1)

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

#!/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中的字符数,将其除以2,并使用20减去该数量的空格填充到左侧。 请注意使用带有否定参数的%*s printf格式说明符来指示左对齐位置。然后将结果用于固定宽度为75的字符串。