I'm trying to make a stopwatch, and when user press Q I want to exit.
I found two script, one where a clock is displayed until ctrl + z is pressed. And one script that exit if "q" is pressed.
I've tried to combined them, but "read" seems to mess it all up.
The reason I want to achieve this is that if the user press Q the time elapsed will be saved to a file.
BEGIN=$(date +%s) echo Starting Stopwatch... while true; do NOW=$(date +%s) let DIFF=$(($NOW - $BEGIN)) let MINS=$(($DIFF / 60)) let SECS=$(($DIFF % 60)) let HOURS=$(($DIFF / 3600)) let DAYS=$(($DIFF / 86400)) # \r is a "carriage return" - returns cursor to start of line printf "\r%3d Days, %02d:%02d:%02d" $DAYS $HOURS $MINS $SECS sleep 0.25 done
Exit on q:
while true; do echo -en "Press Q to exit \t\t: " read input if [[ $input = "q" ]] || [[ $input = "Q" ]] then break else echo "Invalid Input." fi done
PS: I'm very new to this.