# | スクリプト | 解説 | |
---|---|---|---|
1 | #! /bin/bash | # | |
2 | # @(#) justify text / usage: just | # | |
3 | # | ||
4 | # option check and set formatter | # | |
5 | while getopts :r:l:c: opt | # オプションの解析(r,l,c およびその引数) | |
6 | do | # | |
7 | case $opt in | # | |
8 | r) flg=$opt; n=$OPTARG; fmt="printf \"%${n}s\\n\"" ;; | # r の時の書式は %ns の形式, nはオプションに続く引数 | |
9 | l) flg=$opt; n=$OPTARG; fmt="printf \"%-${n}s\\n\"";; | # l の時は同上で、%-ns の形式 | |
10 | c) flg=$opt; n=$OPTARG;; | # c の時は引数を退避しておき、いったん解析は終了。 | |
11 | *) echo "usage: $0 [-rcl WIDTH]"; exit 1;; | # 上記以外のオプションは使いかたを表示して終了 | |
12 | esac | # | |
13 | done | # | |
14 | # | ||
15 | # print stdin with format | # | |
16 | while read buf | # 標準入力から1行とりこみ、buf に記憶 | |
17 | do | # | |
18 | if [[ $flg = 'c' ]]; then | # センタリングの場合は、随時書式を設定 | |
19 | let n1=n/2 | # まず全体の幅の半分を n1 とする | |
20 | let n2=${#buf}/2 | # 入力した buf の文字列長さの半分を n2 とする | |
21 | let n3=n1+n2 | # n1, n2 の和を n3 とする。 | |
22 | fmt="printf \"%${n3}s\\n\"" | # %n3s を書式とする | |
23 | fi | # | |
24 | eval "$fmt \"$buf\"" | # 書式(printf 相当)を評価 | |
25 | done | # |
$ cat /etc/shells | just -r 60 /bin/sh /bin/bash /sbin/nologin /bin/ash /bin/bsh /bin/ksh /usr/bin/ksh /usr/bin/pdksh /bin/tcsh /bin/csh /bin/zsh $ just -c 30 < /etc/shells /bin/sh /bin/bash /sbin/nologin /bin/ash /bin/bsh /bin/ksh /usr/bin/ksh /usr/bin/pdksh /bin/tcsh /bin/csh /bin/zsh