Home > Software engineering >  How to increase an integer variable with another integer variable in bash?
How to increase an integer variable with another integer variable in bash?

Time:11-13

What I need is to increase the value of a variable dynamically (within a loop) by another variable. Here is my code:

while read  Plate City Town Village Area Population; do
    echo "$Plate $City $Town $Village $Area $Population"
    
     printf "\n$totalNumberOfTowns\n " 
     totalNumberOfTowns=$totalNumberOfTowns $Town
     totalNumberOfVillages=$totalNumberOfVillages $Village

done < "cities.txt"

what I get when I run my code is a little bit not-correct. enter image description here

I didn't understand why there is "Town" inside $totalNumberOfTowns variable. Another interesting part is it also does calculation(it sums third parameter which is number of town in that city. For ADANA it is 15 and for ADIYAMAN it is 9 and so on.).

Edit:

First line of cities.txt is:

Plate City Town Village Area Population

see screenshot:
enter image description here

Edit-2: # I have tried these different syntaxes:

let "totalNumberOfTowns =$Town" #1 I tried these different syntaxes
totalNumberOfTowns=$((expr $totalNumberOfTowns   $Town)) #2
totalNumberOfTowns=$((totalNumberOfTowns   Town)) #3

Yet, each of them gave me some errors.

Here are sample cities.txt and sample code:

Plate City Town Village Area Population 1 ADANA 15 508 14030 2258718 2 ADIYAMAN 9 420 7614 632459 3 AFYONKARAHİSAR 18 395 14230 736912 4 AĞRI 8 566 11376 535435 5 AMASYA 7 352 5520 335494 6 ANKARA 25 711 25706 5663322 7 ANTALYA 19 545 20723 2548308 34 İSTANBUL 40 166 5196 15462452

code

#!/usr/bin/env bash

average=0
numberOfCities=80

declare -i totalNumberOfTowns=0 totalNumberOfVillages=0
arrayWithOut=""

while read  Plate City Town Village Area Population; do
    echo "$Plate $City $Town $Village $Area $Population"

    printf "\n$totalNumberOfTowns\n " 
      totalNumberOfTowns =$Town
     totalNumberOfVillages =$Village

done < "cities.txt"

CodePudding user response:

You have to ignore first line.

#!/usr/bin/env bash

average=0
numberOfCities=80

declare -i totalNumberOfTowns=0 totalNumberOfVillages=0
arrayWithOut=""

{
read -r first_line
while read -r Plate City Town Village Area Population; do
    echo "$Plate $City $Town $Village $Area $Population"

    printf "\n$totalNumberOfTowns\n " 
      totalNumberOfTowns =$Town
     totalNumberOfVillages =$Village

done 
} < "cities.txt"

CodePudding user response:

You can also use variables with an integer attribute:

#!/usr/bin/env bash

declare -i totalNumberOfTowns=0 totalNumberOfVillages=0

while read Plate City Town Village Area Population; do
    printf "%s %s %s %s %s %s\n" "$Plate" "$City" "$Town" "$Village" "$Area" "$Population"
    totalNumberOfTowns =$Town
    totalNumberOfVillages =$Village
done < "cities.txt"

printf "Total towns: %d\nTotal villages: %d\n" "$totalNumberOfTowns" "$totalNumberOfVillages"

CodePudding user response:

I know even three ways to calculate in bash:

Command expr (operators: , -, *, /, %, (, ); and logic operators: <, <=, ==, !=, >=, >)

expr 2 * 3 (the spaces and '' sign before operator are important)

or

a=5; a=$(expr $a 1)

Syntax (( <expression> )) - all operators known in C language

a=0; a=$((a 1))

Command let

x=0; let "x = 2"

CodePudding user response:

Just place the expression in double parenthesis for example $((your_expression))

while read  Plate City Town Village Area Population; do
    echo "$Plate $City $Town $Village $Area $Population"
    
     printf "\n$totalNumberOfTowns\n " 
     totalNumberOfTowns=$((totalNumberOfTowns   Town))
     totalNumberOfVillages=$((totalNumberOfVillages   Village))

done < "cities.txt"
  • Related