LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-09-2020, 04:35 PM   #1
Pinguino99
Member
 
Registered: Dec 2019
Posts: 36

Rep: Reputation: Disabled
[Shell Script] - the digits appear wrong in the printf


how do I make the countdown digits appear right?
if I use only $1 it doesn't work correctly because the colon don't appear

Code:
#!/bin/bash

# usage 
# run script specifying hour, minute and second, example of a five-minute countdown: ./countdown.sh 00:05:00

function countdown
{
cols=$( tput cols )
rows=$( tput lines )
middle_row=$(( $rows / 2 ))
middle_col=$(( ($cols /2) - 4 ))

		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))
                tput cup $middle_row $middle_col
				echo -ne "$(printf %02d:%02d:%02d)\e" \
						$((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

                sleep 1
        done
        IFS="${OLD_IFS}"
        echo "        "
}
clear

countdown "$1"
 
Old 09-09-2020, 06:53 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,408

Rep: Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551
Code:
 
tput cup $middle_row $middle_col
echo -ne "$(printf %02d:%02d:%02d)\e" $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))
The echo is redundant plus the \e and the end of the printf does not do what you think it does.

tput cup $middle_row $middle_col places the cursor at the location where you want to display the time so all you need is the printf.
Code:
  printf "%02d:%02d:%02d" $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Shell Script to check if a string has only alphabets and digits. aswani Programming 8 08-16-2012 09:49 AM
[SOLVED] printf $"Hello $var\n" vs. printf "Hello $var\n" -- not a typo. What is it? GrapefruiTgirl Programming 2 10-21-2010 08:21 AM
Karatsuba Wrong Answer After 6 digits Decimal Peter_APIIT Programming 0 05-02-2009 05:18 AM
BASH - convert single digits to double digits. rickenbacherus Programming 7 05-07-2008 06:53 AM
How is 'man 3 printf' different from 'man printf' ?? purpleburple Linux - General 3 09-23-2002 12:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration