error de sintaxis cerca del token inesperado `} '


0

Estoy escribiendo un programa con algunos puntos de referencia aleatorios que mi hermano está pidiendo. Pero cuando terminé de escribirlo, lo probé y aquí está el resultado:

./benchmarksuite.sh: line 45: syntax error near unexpected token `}'
./benchmarksuite.sh: line 45: `}'

Entonces, ¿qué demonios está mal con mi código?

Aquí está el código:

#!/bin/bash

drivetest()
{
        echo "How much data do you want to write (in MiB):"
        read data
        echo "Are you sure you want to perform a $data MiB write onto your disk?[Y/N]"
        read confirm
        if [ "$confirm" == "Y" ]; then
                dd bs=1M count=$data if=/dev/zero of=test conv=fdatasync
        elif [ "$verify" == "y" ]; then
                dd bs=1M count=$data if=/dev/zero of=test conv=fdatasync
        else
                echo "Exiting the test."
        fi


stresstest()
{
        echo "How long is your stress(tips: Use letters along numbers like s,m,h. If you type number with OUT letter, default as second):"
        read time
        echo "How many CPU workers do you want?"
        read cpu_worker
        sudo stress --cpu $cpu_worker --timeout $time
        echo ""
        echo -e "${yellow}Thermal results:${res}"
        sensors
}

random()
{
        echo "           MENU             "
        echo "Type 1 for a drive test.    "
        echo "Type 2 for a stress test.   "
        echo "Type 3 to perform all tests."
        echo "Type other stuffs to exit.  "
        read value
        if [ "$value" == "1" ]; then
                drivetest
        if [ "$value" == "2" ]; then
                stresstest
        if [ "$value" == "3" ]; then
                {
                        drivetest
                        stresstest
                }
}



yellow='\033[1;33m'
res='\033[0m'
echo -e "${yellow}Warning:${res}"
echo -e "${yellow}This script is depended on stress-ng and lm-sensors. Please be sure that you have those package installed.${res}"
echo "Verify [Y/N]:"
read verify
if [ "$verify" == "Y" ]; then
        random
elif [ "$verify" == "y" ]; then
        random
else
        echo -e "${yellow}Please install the dependencies.${res}"
fi

Respuestas:


8

Entonces, ¿qué demonios está mal con mi código?

Para verificar sus scripts de shell, puede usar ShellCheck . Aquí está el resultado de verificar su código:

ingrese la descripción de la imagen aquí

  1. Te faltan algunos fis.

    Agregar el fis genera un nuevo error:

    ingrese la descripción de la imagen aquí

  2. drivetest() parece faltar un cierre }

    Corregir esto te deja con algunas advertencias (que dejaré para que arregles):

    ingrese la descripción de la imagen aquí

Código corregido:

#!/bin/bash

drivetest()
{
        echo "How much data do you want to write (in MiB):"
        read data
        echo "Are you sure you want to perform a $data MiB write onto your disk?[Y/N]"
        read confirm
        if [ "$confirm" == "Y" ]; then
                dd bs=1M count=$data if=/dev/zero of=test conv=fdatasync
        elif [ "$verify" == "y" ]; then
                dd bs=1M count=$data if=/dev/zero of=test conv=fdatasync
        else
                echo "Exiting the test."
        fi
}


stresstest()
{
        echo "How long is your stress(tips: Use letters along numbers like s,m,h. If you type number with OUT letter, default as second):"
        read time
        echo "How many CPU workers do you want?"
        read cpu_worker
        sudo stress --cpu $cpu_worker --timeout $time
        echo ""
        echo -e "${yellow}Thermal results:${res}"
        sensors
}

random()
{
        echo "           MENU             "
        echo "Type 1 for a drive test.    "
        echo "Type 2 for a stress test.   "
        echo "Type 3 to perform all tests."
        echo "Type other stuffs to exit.  "
        read value
        if [ "$value" == "1" ]; then
                drivetest
        fi
        if [ "$value" == "2" ]; then
                stresstest
        fi
        if [ "$value" == "3" ]; then
                {
                        drivetest
                        stresstest
                }
        fi
}



yellow='\033[1;33m'
res='\033[0m'
echo -e "${yellow}Warning:${res}"
echo -e "${yellow}This script is depended on stress-ng and lm-sensors. Please be sure that you have those package installed.${res}"
echo "Verify [Y/N]:"
read verify
if [ "$verify" == "Y" ]; then
        random
elif [ "$verify" == "y" ]; then
        random
else
        echo -e "${yellow}Please install the dependencies.${res}"
fi

ShellCheck: una herramienta de análisis estático de script de shell

ShellCheck es una herramienta GPLv3 que ofrece advertencias y sugerencias para los scripts de shell bash / sh:

Captura de pantalla de un terminal que muestra líneas problemáticas de script de shell resaltadas.

ingrese la descripción de la imagen aquí

Los objetivos de ShellCheck son

  • Para señalar y aclarar problemas típicos de sintaxis de principiante que hacen que un shell dé mensajes de error crípticos.

  • Señalar y aclarar problemas semánticos típicos de nivel intermedio que hacen que un shell se comporte de forma extraña y contraintuitiva.

  • Para señalar advertencias sutiles, casos de esquina y escollos que pueden causar que un script avanzado de otro usuario falle en circunstancias futuras.

Fuente ShellCheck

Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.