2016-04-10 12 views
0

Tüm sunucularım için kullandığım standart bir banner'ım var ve bunları genellikle el ile girdim, ancak sunucu filomuz büyüdükçe, bunu otomatik hale getirme zamanı.Sabit genişlikli bir dizede bir metin satırı nasıl ortalanır?

Tipik olarak, benim afiş gibi görünmelidir:

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

Bu istenen etki, ama benim ana bilgisayar adları karakterleri değişen, bu yüzden bazen, örneğin doğru biçimlendirmek değildir:

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

ve daha kısa hostname ise:

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

Senaryomun kullanmak alakalı kodu oluşturmak için bu geçerli: I veya ana makine adının uzunluğu ne olursa olsun, ilk örnekte verildiyse aynen afiş oluşturmak için en iyi araç olarak ne nasıl kullanılacağını emin değilim

# 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 

? Fikirler için, "çerçeve" (yıldız karakterleriyle gösterildiği gibi) sabit bir değer olmalıdır.

+0

'$ {# hostname}' ile başlamak istediğiniz alanı hesaplayabilirsiniz. –

cevap

1

Bu snippet'indeki parametrelerle etrafında oynayabilir:

#!/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 karakter sayısını almak için ${#h} kullanır

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

gibi çıkış verir ikişer olduğunu böler, hangi ve sola kaydırmak için bu boşluk sayısını 20 eksi kullanır. Bir negatif bağımsız değişkenle %*s Printf biçim belirteci kullanılması sol göstermek için Not yerleştirme haklı. Sonuç daha sonra sabit genişlikte 75 karakter dizisi için kullanılır.

+0

Teşekkürler! Bu çok yardımcı oldu ve çalışmasını sağlamak için ihtiyacım olan şeyi toplayabildim. – digilink

İlgili konular