Template for formatting output as a dynamic table in bash
-2
votes
1
answer
603
views
I have a created a simple script which is using the ps aux command to give information for a process entered by a user, and presents the output in a table like format (something like the mysql shell is doing for table format).
This is working to some extent, the only problem is how to make the cells fit the content dynamically depending on the value's length. If a value is too long, it goes on a new line and breaks the table.
Is there some smarter way to wrap the value within its cell?
#!/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
Current output from above:
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 |
+------------+------------+------------+------------+------------+----------------------------+
Desired output on smaller screens, something like this:
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 |
+------------+------------+------------+------------+------------+-----------------------------------+
Asked by Avocado
(11 rep)
Dec 28, 2023, 12:20 PM
Last activity: Jan 1, 2024, 02:45 AM
Last activity: Jan 1, 2024, 02:45 AM