Script BASH não adicionando variáveis na função

0

Meu script com a função de adição não executará o operador de adição (+) com duas variáveis que eu atribuo valores numéricos com base na leitura. As outras funções funcionarão bem usando os outros operadores.

Script:

#!/bin/bash                                              

function addition {                                      
   FNUM1=$1                                              
   FNUM2=$2                                              
   RESULT=$((FNUM1+FNUM2))                               
   echo "RESULT: $RESULT"                                          
}                                                        

function subtraction {                                    
   FNUM1=$1                                              
   FNUM2=$2                                              
   RESULT=$((FNUM1-FNUM2))                               
   echo "RESULT: $RESULT"                                          
}                                                        

function multiplication {                                       
   FNUM1=$1                                              
   FNUM2=$2                                              
   RESULT=$((FNUM1*FNUM2))                               
   echo "RESULT: $RESULT"                                          
}                                                        

function division {                                      
   FNUM1=$1                                              
   FNUM2=$2                                              
   RESULT=$((FNUM1/FNUM2))                               
   echo "RESULT: $RESULT"                                          
}                                                        

clear
echo "Please select a calculation to make!"              
echo "Choose how to you want to calculate two numbers"

COUNTER=0                                                

while [ $COUNTER -eq 0 ]                                 
do                                                       
   echo ""                                               
   echo "1 - addition"                                   
   echo "2 - subtraction"                                
   echo "3 - multiplication"                             
   echo "4 - division"                                   
   echo "5 - QUIT"                                       

   read CHOICE                                           

   case $CHOICE in                                       
      1)                                                 
         echo "YOU CHOSE ADDITION!"                      
         echo "Enter first number: "                     
         read NUM1                                       
         echo "Added by: "                               
         read NUM2                                       
         addition $NUM1 $NUM                             
         ;;                                              
      2)
         echo "YOU CHOSE SUBTRACTION!"                   
         echo "Enter first number: "                     
         read NUM1                                       
         echo "Subtracted by: "                          
         read NUM2                                       
         subtraction $NUM1 $NUM2                         
         ;;                                              
      3)                                                 
         echo "YOU CHOSE MULTIPLICATION!"                
         echo "Enter first number: "                     
         read NUM1                                       
         echo "Multiplied by: "                          
         read NUM2                                       
         multiplication $NUM1 $NUM2                      
         ;;                                              
      4)                                                 
         echo "YOU CHOSE DIVISION!"                      
         echo "Enter first number: "                     
         read NUM1                                       
         echo "Divided by: "                             
         read NUM2                                       
         division $NUM1 $NUM2                            
         ;;                                              
      5)                                                 
         COUNTER=$(( $COUNTER + 1 ))                     
         ;;                                              
      *)                                                 
         echo "You must enter a number from 1 through 5!"
   esac                                                  
done                                                                                                      

Saída:

Please select a calculation to make!

Choose how to you want to calculate two numbers
1 - addition
2 - subtraction
3 - multiplication
4 - division
5 - QUIT
1
YOU CHOSE ADDITION!
Enter first number:
24
Added by:
5
RESULT: 24

Eu quero que a função de adição adicione os valores que são lidos nas variáveis FNUM1 e FNUM2.

    
por D. Hess 28.05.2017 / 19:21

1 resposta

1

Como o ilkkachu comentou, você tem um erro de digitação simples no código de adição:

     echo "Added by: "                               
     read NUM2                                       
     addition $NUM1 $NUM                             

deve ser:

     echo "Added by: "                               
     read NUM2                                       
     addition $NUM1 $NUM2                

Idem as recomendações comentadas para o ShellCheck e set -u para esses tipos de erros. set -u teria lhe dado um erro ao executar o script.

Treat unset variables and parameters other than the special parameters ‘@’ or ‘*’ as an error when performing parameter expansion. An error message will be written to the standard error, and a non-interactive shell will exit.

    
por 29.05.2017 / 00:10