意外标记“}”附近的语法错误

Pet*_*Lac 0 bash shell-script code

我正在编写一个程序,其中包含我兄弟要求的一些随机基准。但是当我写完它时,我去测试它,结果如下:

./benchmarksuite.sh: line 45: syntax error near unexpected token `}'
./benchmarksuite.sh: line 45: `}'
Run Code Online (Sandbox Code Playgroud)

那么我的代码到底有什么问题呢?

这是代码:

#!/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
Run Code Online (Sandbox Code Playgroud)

Dav*_*ill 8

那么我的代码到底有什么问题呢?

要检查您的 shell 脚本,您可以使用ShellCheck。这是检查代码的输出:

在此处输入图片说明

  1. 你少了几个fi

    添加fis 会产生一个新错误:

    在此处输入图片说明

  2. drivetest() 似乎缺少结束语 }

    更正此问题会给您带来一些警告(我将留给您修复):

    在此处输入图片说明

更正的代码:

#!/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
Run Code Online (Sandbox Code Playgroud)

ShellCheck - 一个 shell 脚本静态分析工具

ShellCheck 是一个 GPLv3 工具,可为 bash/sh shell 脚本提供警告和建议:

显示突出显示有问题的 shell 脚本行的终端屏幕截图。

在此处输入图片说明

ShellCheck 的目标是

  • 指出并澄清导致 shell 给出神秘错误消息的典型初学者语法问题。

  • 指出并澄清导致 shell 行为异常和违反直觉的典型中级语义问题。

  • 指出可能导致高级用户的其他工作脚本在未来情况下失败的微妙警告、极端情况和陷阱。

壳检查