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

I've written this bash script . But it throws this error:

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'
Zanna
  • 72,312
Kanat
  • 399
  • 4
  • 6
  • 19

2 Answers2

9

The problem is elif [[ $var -eq 2]], it should be: elif [[ $var -eq 2 ]].

Spacing matters.

The thing is when it see a [[ it looks for closing ]] and it can't find it instead it see 2]] which doesn't have any meaning.

Ravexina
  • 57,256
5

case would be a good choice here to reduce the code.

case $var in
  0) echo "No students" ;;
  1|2|3|4) echo "$var students" ;;
  *) echo "A lot of students" ;;
esac
glenn jackman
  • 18,218