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 Mon 22 Sep 2014, 04:37
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 Posts_count  
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: 4238
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_website 
sunburnt


Joined: 08 Jun 2005
Posts: 5031
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: 3220

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: 2247

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: 4238
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_website 
sunburnt


Joined: 08 Jun 2005
Posts: 5031
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: 230

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: 4351

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: 851
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: 4238
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_website 
sunburnt


Joined: 08 Jun 2005
Posts: 5031
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: 3220

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:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


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