错误在第 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
关键字适用于您的情况,无论如何,您的代码上注明了一些:
-
把
then
放在if
操作员的线上:if [ $weight1 -ge 20 ] && [ $weight1 -le 39 ]; then
-
将变量括在引号中:
if [ "$weight1" -ge 20 ] && [ "$weight1" -le 39 ]; then
-
我建议使用
-a
键,而不是对[
应用程序进行两次调用:if [ "$weight1" -ge 20 -a "$weight1" -le 39 ]; then
注意:错误Syntax error near unexpected token 'then'
主要在少数情况下会出现:
-
当您指定
then
超出大小写if
关键字时:weight1=21 then echo "Weight 1 is bike." else echo "Weight 1 is either bicycle, car, van or lorry." fi
-
指定
then
两次时:if [ "$weight1" -ge 20 -a "$weight1" -le 39 ]; then then echo "Weight 1 is bike."
-
当您将
then
放在不正确的位置时,例如,在else
运算符之后:else then echo "Weight 1 is either bicycle, car, van or lorry." fi
只是
chmod +x test.sh
第一行: 添加#!/bin/bash
然后做:
./test.sh
它正在解决问题