Bash – syntax error near unexpected token `}’

bashcodeshell-script

I'm writing a program with some random benchmarks that my brother is asking for. But when I'm done writing it, I went testing it and here's the result:

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

So what in the world is wrong with my code?

Here's the code:

#!/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

Best Answer

So what in the world is wrong with my code?

To check your shell scripts you can use ShellCheck. Here is the output from checking your code:

enter image description here

  1. You are missing a few fis.

    Adding the fis generates a new error:

    enter image description here

  2. drivetest() appears to be missing a closing }

    Correcting this leaves you with a few warnings (which I will leave for you to fix):

    enter image description here

Corrected code:

#!/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 - A shell script static analysis tool

ShellCheck is a GPLv3 tool that gives warnings and suggestions for bash/sh shell scripts:

Screenshot of a terminal showing problematic shell script lines highlighted.

enter image description here

The goals of ShellCheck are

  • To point out and clarify typical beginner's syntax issues that cause a shell to give cryptic error messages.

  • To point out and clarify typical intermediate level semantic problems that cause a shell to behave strangely and counter-intuitively.

  • To point out subtle caveats, corner cases and pitfalls that may cause an advanced user's otherwise working script to fail under future circumstances.

Source ShellCheck

Related Question