0

I'm trying to modify this script for error handling. I've 2 errors

#!/bin/bash

re='^[0-9]+$'

echo "Scegli l'operazione da eseguire tra addizione (+) sottrazione (-) e moltiplicazione (*)"
read operazione

     until [ $operazione = + ] || [$operazione = - ] || [$operazione = *]
             do
             echo "L'opzione scelta non è corretta"
             read operazione
     done

echo "Ora scegli due numeri"
read numero[1] numero[2]

     until [[ "$numero[1] =~ $re" ]] && [[ "$numero[2] =~ $re" ]]
             do
             echo "I numeri digitati non sono corretti"
             read numero[1] numero[2]

     done

echo "Il risultato è $((${numero[1]}$operazione${numero[2]}))"

~ ~ So the errors are:

./errori_matematica: line 8: [: too many arguments
./errori_matematica: line 8: [*: command not found

then

in the last while if I type for example f or g or anything character thas is not a number at the last expression I receive 0 or just the number I type (for example if i type 54 l and i choose + like operation in the exit i have 54) but i want error if the numbers are not correct. Thank you and sorry for my english ;)

jordanm
  • 33,009
  • 7
  • 61
  • 76
Giammarco
  • 1
  • 2
  • 1
    The spaces around `[` aren't optional. – choroba Oct 11 '22 at 14:45
  • I try but I still receive the same error – Giammarco Oct 11 '22 at 14:47
  • 1
    Paste your code into shellcheck.net till it gets no errors. Also, if this is bash, prefer `[[` to `[`, it will generally save you headache in the long run. Also, try quoting your operands. Maybe what you want is a `case` statement? – Paul Hodges Oct 11 '22 at 14:48
  • 1
    If you really wanted to use an array, use `readarray` to read into it, and then the elements will be `${numero[1]}` and `${numero[2]}`. But really, just use two regular variables instead, like `a` and `b` – tripleee Oct 11 '22 at 14:52

0 Answers0