
バッシュスクリプト
for i in $script_name
do
echo -en "running the script - $i\t - "
exec 3>&1 4>&2
var=$( { time /tmp/scripts/$i 1>&3 2>&4; } 2>&1) # Captures time only
exec 3>&- 4>&-
echo "$var"
done
以下を印刷してください。
running the script - Verify_disk.bash - 1.42
running the script - Verify_yum_list.bash - 10.49
running the script - Verify_size.bash - 2.93
running the script - Verify_mem_size.bash - 0.71
running the script - Verify_disk_size.bash - 2.41
running the script - Verify_wdisk.bash - 1.63
running the script - Verify_cpu.bash - 0.74
$ varはこれらすべての出力を印刷する変数なので、出力をソートしたいと思います。
それではこうなります。
running the script - Verify_disk.bash - 1.42
running the script - Verify_yum_list.bash - 10.49
running the script - Verify_size.bash - 2.93
running the script - Verify_mem_size.bash - 0.71
running the script - Verify_disk_size.bash - 2.41
running the script - Verify_wdisk.bash - 1.63
running the script - Verify_cpu.bash - 0.74
最後のフィールドをソートするために$ varにさらに変更する必要があるものは何ですか?
答え1
プリループを実行して最も長いファイル名を計算し、それを間隔パラメータとして使用します。
longest=0
for file in *.bash
do
[ "${#file}" -gt "$longest" ] && longest=${#file}
done
# ... for your execution loop
printf "running the script - %${longest}s\t- "
printf "%s\n" "$var"
すべてのスクリプトがワイルドカードに含まれているとします*.bash
。必要に応じて調整してください。初期ループは必要な幅を計算します。printf
この変数は、最初にループの各反復のスクリプトフィールドの幅をフォーマットするために使用されますfor
。
答え2
ループの最初の反復では、次の変数の幅がわからないため、スクリプト内で適切な書式を指定できないと思います。 2段階のプロセスはどうですか?
私も時間値の出力を右に揃える自由(個人的な好み)を取りました。
サンプル入力をファイル(動的に生成できない)に保存した後に提案する内容は次のとおりです。
cat yael | awk -F'-' '{printf "%s - %-30s - % 6.2f\n",$1, $2, $3}'
running the script - Verify_disk.bash - 1.42
running the script - Verify_yum_list.bash - 10.49
running the script - Verify_size.bash - 2.93
running the script - Verify_mem_size.bash - 0.71
running the script - Verify_disk_size.bash - 2.41
running the script - Verify_wdisk.bash - 1.63
running the script - Verify_cpu.bash - 0.74
したがって、私のawkを介してスクリプトの出力をパイプする場合...