Bashの動的テーブルのテンプレートとしての出力フォーマット

Bashの動的テーブルのテンプレートとしての出力フォーマット

ps auxコマンドを使用して、ユーザー入力のプロセスに情報を提供し、出力をテーブル形式で表示する単純なスクリプトを作成しました(mysqlシェルでテーブル形式で実行するのと似ています)。

これはある程度機能しますが、唯一の問題は、セルが値の長さに応じて内容に動的に適応するようにする方法です。値が長すぎると、テーブルがラップされて中断されます。

セルに値をラップするよりスマートな方法はありますか?

#!/bin/bash
# Main function
    main() {
        read -p "Enter the name of the process: " process
        output=$(ps aux | awk -v process="$process" '$0 ~ process && !/awk/ {print}')
        
        if [ -n "$output" ]; then
            printf "+------------+------------+------------+------------+------------+----------------------------+\n"
            printf "| %-10s | %-10s | %-10s | %-10s | %-10s | %-100s |\n" "USER" "PID" "%CPU" "%MEM" "START" "COMMAND"
            printf "+------------+------------+------------+------------+------------+----------------------------+\n"
    
            echo "$output" | awk '{ printf "| %-10s | %-10s | %-10s | %-10s | %-10s | %-100s |\n", $1, $2, $3, $4, $9, substr($0, index($0,$11)) }'
    
            printf "+------------+------------+------------+------------+------------+----------------------------+\n"
        else
            echo "No such process found: $process"
        fi
    }
    # Call the main function
    main

上記の現在の出力:

Enter the name of the process: bash
+------------+------------+------------+------------+------------+----------------------------+
| USER       | PID        | %CPU       | %MEM       | START      | COMMAND                                                                                              |
+------------+------------+------------+------------+------------+----------------------------+
| userrt     | 1072       | 0.0        | 0.1        | 09:04      | -bash                                                                                                |
| userrt     | 1438       | 0.0        | 0.0        | 09:04      | bash                                                                                                 |
| userrt     | 1575       | 0.0        | 0.1        | 09:04      | /bin/bash --init-file /home/userrt/.vscode-server/bin/0ee08df0cf4527e40edc9aa28fdety5656bbff2b2/out/vs/workbench/contrib/terminal/browser/media/shellIntegration-bash.sh |
| userrt     | 3255       | 0.0        | 0.0        | 11:59      | /bin/bash ./process_monitoring.sh                                                                    |
| userrt     | 3286       | 0.0        | 0.0        | 11:59      | /bin/bash ./process_monitoring.sh                                                                    |
+------------+------------+------------+------------+------------+----------------------------+

小さな画面では、希望の出力は次のようになります。

Enter the name of the process: bash
+------------+------------+------------+------------+------------+-----------------------------------+
| USER       | PID        | %CPU       | %MEM       | START      | COMMAND                           |
+------------+------------+------------+------------+------------+-----------------------------------+
| userrt     | 1072       | 0.0        | 0.1        | 09:04      | -bash                             |
| userrt     | 1438       | 0.0        | 0.0        | 09:04      | bash                              |
| userrt     | 1575       | 0.0        | 0.1        | 09:04      | /bin/bash --init-file /home/      |
|            |            |            |            |            |  userrt/.vscode-server/bin/       |
|            |            |            |            |            |  0ee08df0cf4527e40edc9aa28fdety   |
|            |            |            |            |            |  5656bbff2b2/out/vs/workbench/    |
|            |            |            |            |            |  contrib/terminal/browser/media/  |
|            |            |            |            |            |  shellIntegrtion-bash.sh          |
| userrt     | 3255       | 0.0        | 0.0        | 11:59      | /bin/bash ./process_monitoring.sh |
| userrt     | 3286       | 0.0        | 0.0        | 11:59      | /bin/bash ./process_monitoring.sh |
+------------+------------+------------+------------+------------+-----------------------------------+

ベストアンサー1

#!/bin/bash

main() {
    read -p "Enter the name of the process: " process
    output=$(ps aux | awk -v process="$process" '$0 ~ process && !/awk/ {print}')

    if [ -n "$output" ]; then
        # Print header
        printf "| %-10s | %-10s | %-10s | %-10s | %-10s | %-80s |\n" "USER" "PID" "%CPU" "%MEM" "START" "COMMAND"
        
        # Print separator
        printf "|%s|\n" "-------------------------------------------------------------------------------------------"
    
        # Print data, use column command for formatting
        echo "$output" | awk '{ printf "| %-10s | %-10s | %-10s | %-10s | %-10s | %-80s |\n", $1, $2, $3, $4, $9, substr($0, index($0,$11)) }' | column -t -s "|" | fold -w 80 -s
    else
        echo "No such process found: $process"
    fi
}

main

exit 0

出力が渡され、column列の幅が自動的に調整されます。-tテーブルを指定して-s「|」オプションで区切り記号を設定します。

コンテンツの長さに応じた動的テーブルレイアウト。

おすすめ記事