Score:0

What is the difference between these two expressions [Shell Script]

gd flag

A newbie's here. Well, I'm having some trouble understanding the difference between:

let a=$a+$b
let i=$i+1 

and:

a+=$b
i=$(($i + 1))

There's definitely a difference because the second expression ruined my script, giving a trash value.

Here is my script:

the question is [ the script needs to give to the user the ability to put some marks as input until the user give q ( as exit ) or a negative value, in the end, we need to calculate the middle value of the given marks ]

this script is working but if you change let moyenne=$moyenne+$note and let i=$i+1 with moyenne+=$note and i=$(($i + 1)) it will give a trash value for moyenne (the middle value).

#!/bin/bash

note=0
meyenne=0
i=0

until [ "$note" -lt 0 ]; do
  read -p "Entrer la note (appuyez sur q pour quitter): " note
  if [ "$note" = "q" ]; then
    note=-4
    echo "Exit"
  elif [ "$note" -ge 16 ]; then
    echo "Tres Bien"
  elif [ "$note" -ge 14 ]; then
    echo "Bien"
  elif [ "$note" -ge 12 ]; then
    echo "Assez bien"
  elif [ "$note" -ge 10 ]; then
    echo "Moyen"
  elif [ "$note" -ge 0 ]; then
    echo "Insuffi"
  else
    echo "Exit"
  fi
  if [ "$note" -ge 0 ]; then
    let moyenne=$moyenne+$note
    let i=$i+1
  fi
done
moyenne=$(($moyenne / $i))
echo "la moyenne est $moyenne de $i notes"

waltinator avatar
it flag
Always paste your script into `https://shellcheck.net`, a syntax checker, or install `shellcheck` locally. Make using `shellcheck` part of your development process.
OUSSAMA ABBOU avatar
gd flag
@waltinator thank you
Score:3
hr flag

The relevant section of man bash:

   In  the context where an assignment statement is assigning a value to a
   shell variable or array index, the += operator can be used to append to
   or  add  to  the variable's previous value.  This includes arguments to
   builtin commands such as  declare  that  accept  assignment  statements
   (declaration commands).  When += is applied to a variable for which the
   integer attribute has been set, value is evaluated as an arithmetic ex‐
   pression and added to the variable's current value, which is also eval‐
   uated.  When += is applied to an array variable using compound  assign‐
   ment  (see  Arrays  below), the variable's value is not unset (as it is
   when using =), and new values are appended to the  array  beginning  at
   one  greater  than  the  array's  maximum index (for indexed arrays) or
   added as additional key-value pairs in an associative array.  When  ap‐
   plied  to  a  string-valued variable, value is expanded and appended to
   the variable's value.

Since you haven't set the integer attribute for a, a+=$b will perform string concatenation instead of arithmetic addition:

$ a=1; b=2; a+=$b; echo "$a"
12

whereas

$ unset a b
$ declare -i a=1; b=2; a+=$b; echo "$a"
3

Alternatively, you can force arithmetic evaluation using (( ... ))

$ unset a b
$ a=1; b=2; ((a+=$b)); echo "$a"
3

(note that ((a+=b)) also works; the $ isn't necessary to dereference variables in an arithmetic context).

OUSSAMA ABBOU avatar
gd flag
thank you, now I get it.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.