0

Intento hacer una calculadora que sume y reste, muy sencilla. Creo yo que todo mi script esta buena, pero por alguna razon aparece este error: "syntax error: unexpected end of file". No se como solucionarlo. Ocupo ayuda.

#!/bin/bash

#Bienvenida
echo "Welcome"

#Selección
echo -n "Press 1 to add and 2 to substract: "
read operand

#Condicionales
if operand==1
        echo -n "Enter your first number: "
        read number1
        echo -n "Enter your second number: "
        read number2
        echo "The add of your numbers is: $number1+$number2"]
[elif operand==2
        echo -n "Enter your first number: "
        read number1
        echo -n "Enter your second number: "
        read number2
        echo "The substract of your numbers is: $number1-$number2"]
[elif operand<1
        echo "Invalid InPut"]
[elif operand>2
        echo "Invalid InPut"]
2
  • Recomiendo pegar el código en shellcheck.net
    – fedorqui
    el 18 abr. 2020 a las 17:33
  • 1
    Son demasiados errores los que tienes a tal punto que ayudarte equivale a reescribir todo tu código y explicarte paso por paso. En ese caso sería mejor que trataras de ejecutar tu código cada ciertos bloques, incluso en modo interactivo en tu terminal, y luego volvieras con en busca de respuestas concretas.
    – Cuauhtli
    el 19 abr. 2020 a las 0:06

1 respuesta 1

0

Principalmente tenias problemas con las sentencias if...elif omitiendo los then, entre otras cosas... Te dejo un ejemplo de lo que seria un bloque de expresiones con if:

if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi

Me tome el tiempo de hacer las correcciones necesarias a tu script y ahora funciona bien. Aca lo tienes:

            #!/bin/bash
            echo "Welcome"
            #Selección
            echo -n "Press 1 to add and 2 to substract: "
            read -r operand
            #Condicionales
            if [ "$operand" == 1 ]
            then
                    echo -n "Enter your first number: "
                    read -r number1
                    echo -n "Enter your second number: "
                    read -r number2
                    echo "The add of your numbers is: $((number1 + number2))"
            elif [ "$operand" == 2 ]
            then
                    echo -n "Enter your first number: "
                    read -r number1
                    echo -n "Enter your second number: "
                    read -r number2
                    echo "The substract of your numbers is: $((number1 - number2))"
            elif [ "$operand" -lt 1 ]
            then
                    echo "Invalid InPut"
            elif [ "$operand" -gt 2 ]
            then
                    echo "Invalid InPut"
            else
                    echo "Bye"
            fi
2
  • @gabrielcoronel recuerda aceptar la respuesta o dar voto a favor si fue de tu utilidad. En este Enlace te explica como hacerlo. Saludos!
    – Jbeltran
    el 19 abr. 2020 a las 12:08
  • Muchas gracias, es que soy muy manco para estas cosas @Jbeltran
    – user166844
    el 19 abr. 2020 a las 15:41

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.