Bash:意外标记"then"附近的语法错误



错误在第 21 行...

if [ $weight1 -gt $weight2 ];
    then echo "Weight 2 should be greater than Weight 1."
else
    if [ $weight1 -ge 20 ] && [ $weight1 -le 39 ];
        then echo "Weight 1 is bike."
    else 
        echo "Weight 1 is either bicycle, car, van or lorry."
    fi
fi

我完成了\没有看到问题,在我的测试中then关键字适用于您的情况,无论如何,您的代码上注明了一些:

  1. then放在if操作员的线上:

    if [ $weight1 -ge 20 ] && [ $weight1 -le 39 ]; then
    
  2. 将变量括在引号中:

    if [ "$weight1" -ge 20 ] && [ "$weight1" -le 39 ]; then
    
  3. 我建议使用-a键,而不是对[应用程序进行两次调用:

    if [ "$weight1" -ge 20 -a "$weight1" -le 39 ]; then
    

注意:错误Syntax error near unexpected token 'then'主要在少数情况下会出现:

  1. 当您指定then超出大小写if关键字时:

    weight1=21
        then echo "Weight 1 is bike."
    else
        echo "Weight 1 is either bicycle, car, van or lorry."
    fi
    
  2. 指定then两次时:

    if [ "$weight1" -ge 20 -a "$weight1" -le 39 ]; then
    then echo "Weight 1 is bike."
    
  3. 当您将then放在不正确的位置时,例如,在else运算符之后:

    else
    then
        echo "Weight 1 is either bicycle, car, van or lorry."
    fi
    

只是

chmod +x test.sh

第一行: 添加#!/bin/bash

然后做:

./test.sh

它正在解决问题

相关内容

  • 没有找到相关文章

最新更新