Artificial intelligent assistant

Build a time elapse function i want to build a time elapse function inside a script to print that time every second for example the output should look like below: finding files inside a directory time elapse HH:MM:SS * HH is hour * MM is minute * SS is second and the line time elapse etc keeps counting up 1 second on the screen without printing a new line after the script is finished executing the function stops.

I would write a stopwatch function, like the one I have posted here, and call it telling it to watch the process id of the command you're running (note that this assumes GNU `date`):


#!/bin/bash

stopwatch(){
date1=`date +%s`;
while kill -0 $1 2>/dev/null; do
printf "Elapsed time: %s\r" "$(date -u --date @$((`date +%s` - $date1)) +%H:%M:%S)";
sleep 0.1
done
}

find /path/to/dir > output 2> error &
stopwatch $!


Note that this approach means that if you kill the script with `Ctrl`+`C`, the `find` process will keep running in the background. Stéphane's solution is a better idea.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 914cf163541f834958301a554e592ce2