
私のスクリプト:
date
echo -e "${YELLOW}Network check${NC}\n\n"
while read hostname
do
ping -c 1 "$hostname" > /dev/null 2>&1 &&
echo -e "Network $hostname : ${GREEN}Online${NC}" ||
echo -e "${GRAY}Network $hostname${NC} : ${RED}Offline${NC}"
done < list.txt
sleep 30
clear
done
次の情報が出力されます。
Network 10.x.xx.xxx : Online
Network 10.x.xx.xxx : Offline
Network 10.x.xx.xxx : Offline
Network 10.x.xx.xxx : Offline
Network 10.x.xx.x : Online
Network 139.xxx.x.x : Online
Network 208.xx.xxx.xxx : Online
Network 193.xxx.xxx.x : Online
次のようにまとめたいです。
Network 10.x.xx.xxx : Online
Network 10.x.xx.xxx : Offline
Network 10.x.xx.xxx : Offline
Network 10.x.xx.x : Online
Network 139.xxx.x.x : Online
Network 208.xx.xxx.xxx : Online
Network 193.xxx.xxx.x : Online
Network 193.xxx.xxx.xxx : Offline
答え1
フォーマットされた出力の場合printf
(思ったより携帯性が良いecho
)。また、拡張が必要な形式で保存するのではなく、カラーエスケープシーケンスの実際の値を保存しますecho
。
RED=$(tput setaf 1) GREEN=$(tput setaf 2) YELLOW=$(tput setaf 3)
NC=$(tput sgr0)
online="${GREEN}online$NC" offline="${RED}offline$NC"
ping -c 1 "$hostname" > /dev/null 2>&1 && state=$online || state=$offline
printf 'Network %-15s: %s\n' "$hostname" "$state"
%-15s
zsh
長さ(および の文字数でfish
測定)になるように文字列の右に空白を埋める形式仕様です。他のほとんどのシェル/printfのバイト)は15以上です。
$ printf '|%-4s|\n' a ab abc abcd abcde
|a |
|ab |
|abc |
|abcd|
|abcde|
printf '|%4s|\n' a ab abc abcd abcde
| a|
| ab|
| abc|
|abcd|
|abcde|
トリミング:
$ printf '|%.4s|\n' a ab abc abcd abcde
|a|
|ab|
|abc|
|abcd|
|abcd|
$ printf '|%4.4s|\n' a ab abc abcd abcde
| a|
| ab|
| abc|
|abcd|
|abcd|
$ printf '|%-4.4s|\n' a ab abc abcd abcde
|a |
|ab |
|abc |
|abcd|
|abcd|
列のテキストを書式設定するその他のユーティリティは次のとおりです。POSIXexpand
:
printf 'Network %s\t: %s\n' "$hostname" "$state" | expand -t 30
\t
(ここではタブは30列()ごとに拡張され、タブストップが使用されます。)
printf 'Network %s\n: %s\n' "$hostname" "$state" | pr -at2
(ここでは、出力は36列幅の2列です(-w
ページ幅デフォルトの72を変更するオプションを参照)。
またはBSDrs
:
{
while...
printf 'Network %s\n: %s\n' "$hostname" "$state"
done
} | rs -e 0 2
(column
すべての入力を読み出すまで出力が開始されないのと同じです。)
または牛に似た一種の栄養columns
:
printf 'Network %s\n: %s\n' "$hostname" "$state" | columns -w 25 -c 2
zsh
文字列パディングのためのいくつかのパラメータ拡張フラグもあります${(l[15])hostname}
。左パディング${(r[15])hostname}
と正しいパディング(カットを含む)。このフラグが追加されると、m
パディングは文字の表示幅を考慮し、フラグがない場合はすべての文字の表示幅が同じであると想定されます。
存在する急速な拡張(プロンプトまたはフラグを使用したパラメータ拡張でprint -P
有効%
)%F{green}
カラー出力もサポートされているため、次のことができます。
online='%F{green}online%f'
printf '%s\n' "Network ${(r[15])hostname}: ${(%)online}"
または:
print -rP "Network ${(r[15])hostname}: $online"
の内容$hostname
もプロンプト拡張の影響を受けますpromptsubst
が、このオプションが有効になっていて、その内容をユーザーが制御できない場合、これはコマンド注入の脆弱性になります$hostname
。hostname='$(reboot)'
答え2
簡単にcolumn
注文する:
yourscript.sh | column -t
出力:
Network 10.x.xx.xxx : Online
Network 10.x.xx.xxx : Offline
Network 10.x.xx.xxx : Offline
Network 10.x.xx.xxx : Offline
Network 10.x.xx.x : Online
Network 139.xxx.x.x : Online
Network 208.xx.xxx.xxx : Online
Network 193.xxx.xxx.x : Online
答え3
\t
列を移動する場所(タブ文字)に数値セットを挿入するようにスクリプトを更新します。
次のような出力が希望のソートを提供します。
Network 10.x.xx.xxx\t: Online
Network 10.x.xx.xxx\t: Offline
Network 10.x.xx.xxx\t: Offline
Network 10.x.xx.xxx\t: Offline
Network 10.x.xx.x\t: Online
Network 139.xxx.x.x\t: Online
Network 208.xx.xxx.xxx\t: Online
Network 193.xxx.xxx.x\t: Online
答え4
@Romanよりはるかに優れています。
yourscript.sh | column -t -s $'\t'
次に、各行を追加して列に分割\t
します。