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 |
+------------+------------+------------+------------+------------+-----------------------------------+
ps auxthat you need to parse to look for the name of a process in. It should include partial substring matches (foowhenfoobaris present), no matches (foowhen not present), full string matches, matches against other parts of theps auxoutput (foois ingrep foo) so we can see how you want all of those handled and so we have something we can copy/paste to test a potential solution against and get a simple pass/fail result if it produces the output you show from the input you show.bashbut a user namedbash(short for Sebastian) was running a process namedgrepor anyone was running a process namedfoobashellor you wanted to find a process namedawk, or various other things so make sure to cover all of those in your sample input/output.