Division, Addition, Subtraction and Multiplication

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

Division, Addition, Subtraction and Multiplication

#1 Post 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:
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#2 Post by slavvo67 »

Maybe this link will help. Different format but it appears to work.

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

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

Re: Division, Addition, Subtraction and Multiplication

#3 Post 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

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

Re: Division, Addition, Subtraction and Multiplication

#4 Post 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?
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#5 Post 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.

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#6 Post 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.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

Post Reply