Aktionen
Bash countdown¶
This uses a few tricks to do what you want:
- The printf command does NOT append a linefeed automatically unless you tell it to.
- Setting IFS to something else lets the shell split things apart into arrays on whatever delimiter you want, in this case , :.
- The date command can be used to produce time in seconds.
- Carriage returns return the cursor to the beginning of the line without moving to the next line.
function countdown { local OLD_IFS="${IFS}" IFS=":" local ARR=( $1 ) local SECONDS=$(( (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2] )) local START=$(date +%s) local END=$((START + SECONDS)) local CUR=$START while [[ $CUR -lt $END ]] do CUR=$(date +%s) LEFT=$((END-CUR)) printf "\r%02d:%02d:%02d" \ $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60)) sleep 1 done IFS="${OLD_IFS}" echo " " } countdown "00:07:55"
Von Jeremias Keihsler vor fast 8 Jahren aktualisiert · 1 Revisionen