Addition in bash

Ray Parrish crp at cmc.net
Wed Oct 14 09:54:24 UTC 2009


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
> # 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.
> Minutes=0;
> 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
> # if Duration is blank (unspecified)
> if [ "$Duration" == "" ]
>     then
>            Duration=300; # set default interval of 300 seconds.
> fi
> 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 ]
>   do
> # first sleep for the interval
>   sleep "$Interval";
> # Increment Seconds and Elapsed by the Interval setting
>   Seconds=$(( $Seconds +$Interval ));
>   Elapsed=$(( $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

Later, Ray Parrish


-- 
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com






More information about the ubuntu-users mailing list