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

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

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

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

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

代码如下:

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

答案1

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

要检查你的 shell 脚本,你可以使用壳牌检测。以下是检查代码的输出:

在此处输入图片描述

  1. 您漏掉了几个fis。

    添加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

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

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

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

在此处输入图片描述

ShellCheck 的目标是

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

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

  • 指出细微的警告、极端情况和陷阱,这些可能会导致高级用户的原本可以正常工作的脚本在未来情况下失败。

来源壳牌检测

相关内容