Page 1 of 1

Division, Addition, Subtraction and Multiplication

Posted: Wed 17 Feb 2016, 18:05
by LazY Puppy
Hi.

This works: STEPS=$(( $SECONDS / 100 ))
Also this: STEP=$(( $STEP + 2 ))
This won't work: STEPS=$(( $SECONDS * 60 ))

How to do a multiplication in bash?

Thanks

Edit:

Sorry, the problem was a typo I found yet. :roll:

So this works of course also: STEPS=$(( $SECONDS * 60 )) :oops:

Posted: Thu 18 Feb 2016, 02:00
by slavvo67
Maybe this link will help. Different format but it appears to work.

http://www.murga-linux.com/puppy/viewtopic.php?t=101416

Re: Division, Addition, Subtraction and Multiplication

Posted: Thu 18 Feb 2016, 02:36
by MochiMoppel
LazY Puppy wrote:This won't work: STEPS=$(( $SECONDS * 60 ))
Why not?
So this works of course also: STEPS=$(( $SECONDS * 60 ))
"Of course"? Where is the difference? I understand that you edited your post, but if you also edited your typo the whole post becomes confusing.

BTW: Depending on what you want to calculate your example might produce unexpected results. What do you expect as a result for variable STEP in following example? "Of course" 300 I hope...

Code: Select all

#!/bin/bash
SECONDS=2
sleep 3
STEP=$(( $SECONDS * 60 ))
echo $STEP

Re: Division, Addition, Subtraction and Multiplication

Posted: Thu 18 Feb 2016, 03:40
by LazY Puppy
MochiMoppel wrote:
LazY Puppy wrote:This won't work: STEPS=$(( $SECONDS * 60 ))
Why not?
So this works of course also: STEPS=$(( $SECONDS * 60 ))
"Of course"? Where is the difference? I understand that you edited your post, but if you also edited your typo the whole post becomes confusing.

BTW: Depending on what you want to calculate your example might produce unexpected results. What do you expect as a result for variable STEP in following example? "Of course" 300 I hope...

Code: Select all

#!/bin/bash
SECONDS=2
sleep 3
STEP=$(( $SECONDS * 60 ))
echo $STEP
No, I did NOT edit my typo.

The typo was in the script from where these examples has been taken.

The problem was as SECONDS was created this way::

SECONDS=$(( $SECONDS1 + $SECONDS2 ))

But I had used seconds1 and seconds2 above in the related script.

SECONDS1 (seconds1) was build from the minutes of a time variable return (mp3 tag) (e.g. 03:47)

I just rebuilt minutes plus seconds to just seconds to have a <hscale> widget moving from the left to the right of the GUI to give an optical impression of current song position (I'd created my first Audio Player using GtkDialog; a simple one, just directory and files based).

So seconds1 is different to SECONDS1.

That's just all.

Probably Flash could just remove this topic?

Posted: Thu 18 Feb 2016, 04:42
by MochiMoppel
I'm not much interested in your typo. I only wanted to draw your kind attention to the fact that $SECONDS is a special bash variable like $1, $LINENO or $PWD. You should never use these names for your own variables unless you know what you are doing.

Posted: Thu 18 Feb 2016, 06:38
by LazY Puppy
MochiMoppel wrote:I'm not much interested in your typo. I only wanted to draw your kind attention to the fact that $SECONDS is a special bash variable like $1, $LINENO or $PWD. You should never use these names for your own variables unless you know what you are doing.
Ok, I did not know about $SECONDS being a special bash variable. Going to change this immediately.

Thanks.