我的 bash 脚本有什么问题?

kyr*_*git -4 command-line bash scripts

#!/bin/bash
var=1
if [[ $var -eq 0 ]]
then
    echo "No students"
elif [[ $var -eq 1 ]]
then
    echo "1 student"
elif [[ $var -eq 2]]
then
    echo "2 students"
elif [[ $var -eq 3 ]]
then 
    echo "3 students"
elif [[ $var -eq 4 ]]
then
    echo "4 students"
else
    echo "A lot of students"
fi
Run Code Online (Sandbox Code Playgroud)

我写了这个 bash 脚本。但它抛出这个错误:

Failed test #1. Runtime error:
main.sh: line 11: syntax error in conditional expression
main.sh: line 12: syntax error near `then'
main.sh: line 12: `then'
Run Code Online (Sandbox Code Playgroud)

Rav*_*ina 9

问题是elif [[ $var -eq 2]],应该是:elif [[ $var -eq 2 ]]

间距很重要。

问题是当它看到 a 时,它[[会寻找关闭]],但它找不到它,而是看到2]]它没有任何意义。


gle*_*man 5

case 在这里减少代码将是一个不错的选择。

case $var in
  0) echo "No students" ;;
  1|2|3|4) echo "$var students" ;;
  *) echo "A lot of students" ;;
esac
Run Code Online (Sandbox Code Playgroud)