How to show live results on terminal from a shell script?

you can use the watch(1) command to run your script at regular intervals:

watch -n 1 myscript.sh

This will run myscript.sh every 1 second clearing the screen between each run and with a timestamp in the corner. You can use the -d option and it will even highlight differences in the output per run.


It would help if you were a lot more specific about what you are trying to do.

Here is an extremely simplistic example:

while true
do
    clear
    date
    sleep 1
done

Most of that data is generally exposed in the /proc virtual file-system primitives. Each process has an entry in /proc in a directory called the PID. So /proc/5437 would have the primitives for the 5437 process. Reading the primitives there and parsing appropriately would yet you close to what top does.

Top actually works by calling specific function calls that extract this information directly from the kernel instead of pulling it from files. To do the same from bash you'd have to either pull it from the /proc virtual file system, or extract it out of other calls such as to ps.

As for real-time, that isn't quite doable at the level of detail top provides. You can slice time fine enough that it appears to be real-time, but you will still be getting time-slices.