Hello,

On Mon, 19 Nov 2007, Bryen wrote:
>For all you scripting gurus out there.  Can you help me out?
>
>I'm trying to convert a value to an output to the user of
>minutes:seconds.
>
>For example:
>if $A=100 (for seconds)
>Then echo "This is 1:40 minutes"
>
>How would I do this?

POSIX Shell (e.g. bash, ksh and others):

====
for secondsin in 1 59 61 100 101 10000; do
    temp=$secondsin;
    h=$(( temp / 3600 ));
    if test $h -gt 0; then
        temp=$(( temp - h * 3600 ));
    fi
    m=$(( temp / 60 ));
    s=$(( temp % 60 ));
    printf "%i second(s) are %i hours, %i minutes and %i seconds\n" \
        $secondsin $h $m $s;
done
====

Adjust the output-format as you like, you got the neccessary numbers
neatly seperated in $h, $m and $s.

See "Arithmetic Expansion" or something like that in the manpage of
your shell.

Tested with bash 2.03 (in bash and sh mode), pdksh 5.2 and zsh 3.0.

pdksh and zsh seem to have no 'printf' builtin (/usr/bin/printf is
used), but that should not be a problem.

Any questions?

HTH,
-dnh

-- 
"The Unix phenomenon is scary.  It doesn't go away."  -- Steve Ballmer
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to