Skip to content

Instantly share code, notes, and snippets.

@alexec
Created September 4, 2013 18:04
Show Gist options
  • Save alexec/6440533 to your computer and use it in GitHub Desktop.
Save alexec/6440533 to your computer and use it in GitHub Desktop.
Run a command with as stopwatch, print the seconds as they pass. So you can track the progress of a command that lacks it's own progress bar.
#!/bin/bash
set -ue
BEGIN=$(date +%s)
BACK="\b\b\b\b"
($* ; touch /tmp/$$.done) &
OLDDIFF=0
while true; do
NOW=$(date +%s)
DIFF=$(($NOW - $BEGIN))
MINS=$(($DIFF / 60))
SECS=$(($DIFF % 60))
#only echo count if its different than the last time
if [ "$DIFF" != "$OLDDIFF" ]
then
#backspace 4 times to reset stopwatch position
#The '-e' enables \b to be interpreted correctly
#The '-n' avoids the newline character at the end
echo -ne $BACK
echo -ne $MINS:`printf %02d $SECS`
fi
OLDDIFF=DIFF
sleep 1s
if [ -e /tmp/$$.done ]; then
echo
exit
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment