最後のフィールドの出力をソートする方法

最後のフィールドの出力をソートする方法

バッシュスクリプト

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

おすすめ記事