Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 23 Apr 2014, 17:00
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Bash script variables assignment syntax question
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [13 Posts]  
Author Message
emily22

Joined: 06 May 2012
Posts: 3

PostPosted: Sun 06 May 2012, 05:06    Post subject:  Bash script variables assignment syntax question  

hey there to all!

I'm writing a bash script and I wonder what is the correct syntax for arithmetic expression when it comes to integers which are stored in variables.

For example

aa=23
bbb=35
cccc=479

and I want to assign:

dd=$(($aa+$bbb))

or

aa=$(($aa+$bbb))

or

ee=$(($cccc-$aa))

Is this correct or let is working better?
Back to top
View user's profile Send private message 
musher0


Joined: 04 Jan 2009
Posts: 3810
Location: Gatineau (Qc), Canada

PostPosted: Sun 06 May 2012, 18:57    Post subject:  

Hello!

I know it is never wrong to put a 'let' statement before this type of variable, although it takes an extra 4 bytes.

Best of luck.

_________________
"Logical entities must not be multiplied needlessly." / "Il ne faut pas multiplier les êtres logiques inutilement." (Ockham)
Back to top
View user's profile Send private message Visit poster's website 
sunburnt


Joined: 08 Jun 2005
Posts: 4985
Location: Arizona, U.S.A.

PostPosted: Sun 06 May 2012, 23:50    Post subject:  

Hi emily22; "Let" is kinda diminished, or depreciated as it were ( not used ).

I can`t remember the forum name of the competent member that showed me this:
Code:
aa=23 ; bbb=35

((dd=$aa+$bbb))

echo $dd
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3219

PostPosted: Mon 07 May 2012, 00:56    Post subject:  

sunburnt wrote:
Hi emily22; "Let" is kinda diminished, or depreciated as it were ( not used ).

I can`t remember the forum name of the competent member that showed me this:
Code:
aa=23 ; bbb=35

((dd=$aa+$bbb))

echo $dd


works, but what's wrong with dd=$(($aa + $bbb)) ?
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2171

PostPosted: Mon 07 May 2012, 03:30    Post subject:  

'$((dd=$aa+$bbb))' would be the most portable among different shells. 'let' is bash-specific. While '((dd=$aa+$bbb))' works with bash, it may not with other shells. Whitespace around the operators has o effect.
Back to top
View user's profile Send private message 
musher0


Joined: 04 Jan 2009
Posts: 3810
Location: Gatineau (Qc), Canada

PostPosted: Tue 15 May 2012, 01:22    Post subject:  

Hello.

Even if the "let" statements are officially deprecated, the following script does not work if the "let" statements are removed.

Code:

#!/bin/sh
# ~/$MAPPS/bin/urxvt-fordo-cascade-4.sh
####
# Ce script-ci doit demeurer en l'état.
# (c) Christian L'Écuyer, Gatineau (Qc), Canada, janvier 2012. Gratuiciel, mais tous droits réservés.
# Courriel : http://scr.im/reyucelc
#
# Si ça ne marche pas, assurez-vous qu'un autre "rxvt" ne tourne pas quelque part en mémoire.
####
if [ "`pidof rxvt`" != "" ];then
   for i in `seq 4`;
   do
      wmctrl -c Console
   done
   exit
   else   
      export gm="g 80x16"
      let x=45
      let y=80
      for i in `seq 4`;
           do
                   rxvt -bd grey10 -b 4 -fg white -cr green -sr +st +tcw -bc -tint black -sh 57 -T "Console $i" -$gm-$x-$y &
         let x=$x+120
         let y=$y+170
         sleep 0.17s
           done
fi


Is it me, bash (version 4.1.0-1_i686, 2009), my computer, my Puppy version (lupu 5.25 retro4), my attitude towards life, a spell from my ex? Laughing Someone has an explanation? Thanks in advance.

Also, before you ask, I have found a reason to open 4 console windows at the same time! Very Happy

BFN.

_________________
"Logical entities must not be multiplied needlessly." / "Il ne faut pas multiplier les êtres logiques inutilement." (Ockham)
Back to top
View user's profile Send private message Visit poster's website 
sunburnt


Joined: 08 Jun 2005
Posts: 4985
Location: Arizona, U.S.A.

PostPosted: Tue 15 May 2012, 19:26    Post subject:  

musher0;
Code:
x=45 ; y=80
........
((x=$x+120)) ; ((y=$y+170))
Back to top
View user's profile Send private message 
Peterm321

Joined: 29 Jan 2009
Posts: 209

PostPosted: Tue 15 May 2012, 20:10    Post subject:  

emily22 wrote:
For example

aa=23
bbb=35
cccc=479

and I want to assign:

dd=$(($aa+$bbb))


Code:

#!/bin/bash
aa=23
bbb=35
cccc=479
let dd=aa+bbb
echo -e "\ndd= $dd\n"
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Tue 15 May 2012, 23:28    Post subject:  

musher0 wrote:
Hello.

Even if the "let" statements are officially deprecated, the following script does not work if the "let" statements are removed.

...

Is it me, bash (version 4.1.0-1_i686, 2009), my computer, my Puppy version (lupu 5.25 retro4), my attitude towards life, a spell from my ex? Laughing Someone has an explanation? Thanks in advance.

Also, before you ask, I have found a reason to open 4 console windows at the same time! Very Happy

BFN.
see amigo's post for portable, alternative syntax
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Shep

Joined: 08 Nov 2008
Posts: 842
Location: GIRT-BY-SEA

PostPosted: Sun 20 May 2012, 10:54    Post subject:  

sunburnt wrote:
I can`t remember the forum name of the competent member that showed me this:
Code:
aa=23 ; bbb=35

((dd=$aa+$bbb))

echo $dd

Inside $((....)) bash allows you to omit the $ preface to all the variables:

a=24 b=56 c=$((a+b)); echo $c
80
Back to top
View user's profile Send private message 
musher0


Joined: 04 Jan 2009
Posts: 3810
Location: Gatineau (Qc), Canada

PostPosted: Sun 20 May 2012, 15:03    Post subject:  

Thanks to all for their answers.
_________________
"Logical entities must not be multiplied needlessly." / "Il ne faut pas multiplier les êtres logiques inutilement." (Ockham)
Back to top
View user's profile Send private message Visit poster's website 
sunburnt


Joined: 08 Jun 2005
Posts: 4985
Location: Arizona, U.S.A.

PostPosted: Sun 20 May 2012, 18:27    Post subject:  

Shep; You`re right of course, I was winging it from memory.
So...
Quote:
((dd=aa+bb))
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3219

PostPosted: Sun 20 May 2012, 19:23    Post subject:  

sunburnt wrote:
Shep; You`re right of course, I was winging it from memory.
So...
Quote:
((dd=aa+bb))


interesting; I'd never tried that before. Just goes to prove that $$ is dispensable.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [13 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0634s ][ Queries: 11 (0.0039s) ][ GZIP on ]