Addition in bash

David N. Lombard dnl at speakeasy.net
Wed Oct 14 13:23:36 UTC 2009


Ray Parrish wrote:
> Ray Parrish wrote:
>> Rakotomandimby Mihamina wrote:
>>   
>>> 10/14/2009 10:24 AM, Ray Parrish:
>>>   
>>>     
>>>>    Seconds=$Seconds+5;  [...]
>>>> For some reason Seconds becomes "0+5" instead of the integer value 5
>>>>     
>>>>       
>>> - http://lists.gnu.org/mailman/listinfo/ there is some information about
>>> bash related mailing lists
>>> - http://www.google.com/search?q=bash+arithmetic has some solutions
>>>   
>>>     
>> Thanks for the tips, I found a good article that explained integer math, 
>> and have the completed script working as shown below -
>>
>> #!/usr/bin/env bash

#!/bin/bash

env is used when you don't know the absolute path of the binary.  bash 
is /bin/bash--to move it elsewhere would cause intense carnage.

>> # Usage: Timer.sh n n
>> # where n and n are numeric values expressed in seconds. The first n is 
>> the interval of how often you would like to see updates to the timer 
>> display.
>> # The second n is the duration you would like th''''''''''''''''''e 
>> timer to run before shutting off.
>> # To stop the script at any point execute a CTRL-Z in Terminal. By doing 
>> this you can use the Timer script as a stop watch, just set a long
>> # enough duration to cover the event length you are attempting to time, 
>> and hit CTRL-Z to stop timing when the event is over.

You want <Ctrl-C>.  <Ctrl-Z> "stops" (read suspends) the job; you would 
then typically type "bg" to let the job resume executing in the 
background (as if you have suffixed the original command with a "&") or 
type "fg" to bring it back to the foreground.  If you've been using 
<Ctrl-Z>, you probably have a few things suspended.  Type "jobs" and see 
what it shows.

>> Minutes=0;

The semicolon is extraneous.  Don't program bash in C.

>> Seconds=0;
>> Elapsed=0;
>> Duration=$2;
>> Interval=$1;
>> # if Interval is blank (unspecified)
>> if [ "$Interval" == "" ]
>>     then
>>             Interval=1; # set default interval of one second is set
>> fi

Interval=${1:-1}

>> # if Duration is blank (unspecified)
>> if [ "$Duration" == "" ]
>>     then
>>            Duration=300; # set default interval of 300 seconds.
>> fi

Duration=${2:-300}

>> echo "$Interval Interval"; # echo interval setting
>> echo "$Duration Duration": # echo Duration setting
>> # While elapsed time is less than the Duration setting do loop
>> while [ $Elapsed -lt $Duration ]
>> # first sleep for the interval
>>   sleep "$Interval";
>> # Increment Seconds and Elapsed by the Interval setting
>>   Seconds=$(( $Seconds +$Interval ));
>>   Elapsed=$(( $Elapsed +$Interval ));

(( Seconds += Interval ))
(( Elapsed += Interval ))

>> # if Seconds equals 60
>>   if [ $Seconds == 60 ]
>>       then
>>               Seconds=0; # reset Seconds to zero
>>               Minutes=$(( $Minutes + 1)); # add one to the Minutes value
>>   fi
>> # clear the screen between each timer update
>>   clear
>> # Update the timer display to the current value
>>   echo "Elapsed Time: $Minutes Minutes $Seconds Seconds"
>> done
>> exit
>>
>> I do not know why padsp espeak would not work from within terminal, as 
>> it works just fine in scripts I write within Kalarm.
>>
>> Later, Ray Parrish
>>   
> OOPS! Here is a small correction that allows it to work correctly when 
> the interval doesn't divide evenly into 60 seconds.
> 
> # if Seconds equals 60 or is greater than 60 seconds
>   if [ $Seconds == 60 ] || [ $Seconds -gt 60 ]
>       then
>               Seconds=$(( $Seconds - 60)); # trim 60 seconds off the 
> reading and
>               Minutes=$(( $Minutes + 1)); # add one to the Minutes value
>   fi

   if [[ $Seconds -ge 60 ]] ; then

or

   [[ $Seconds -ge 60 ]] && (( Seconds -= 60 )) && (( Minutes += 1 ))

-- 
David N. Lombard




More information about the ubuntu-users mailing list