2016-04-10 6 views
0

私はすべてのサーバーに使用する標準的なバナーを持っていますが、通常は手動で入力していますが、サーバーの数が増えているので、これを自動化するときです。固定幅の文字列にテキスト行をどのように配置するには?

一般的に、私のバナーは次のようになります。

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

?アイデアを探して、 "フレーム"(アスタリスク文字で示される)は固定値でなければなりません。あなたはこのスニペットのパラメータで遊ぶことができます

+0

'$ {#hostname}'で始まるスペースの量を計算できます。 –

答えて

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 

$h内の文字数を取得するために${#h}を使用しています

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

のような出力を与え、2であることを分割しますその数のスペースから20を差し引いて左に詰め込む。 負の引数を持つ%*sのprintf形式指定子を使用すると、が左寄せのの配置を示すことに注意してください。その結果は、固定幅の75文字ストリングに対して使用されます。

+0

ありがとうございます!これは非常に役に立ち、私はそれを機能させるために必要なものを集めることができました。 – digilink

関連する問題