Abhishta Gatya
Abhishta Gatya

Reputation: 923

Bash : Syntax error near unexpected token `elif' with if/then blocks w/ only comments

I was putting comments on the if statements, just to be a placeholder, then I try to run it and ... I got a syntax error :

syntax error near unexpected token `elif'

`elif [ $1 = "done" || $1 = "-d" ]; then'

#!/bin/bash

if [[ $1 = "add" || $1 = "-a"  ]]; then
  # statements
elif [[ $1 = "done" || $1 = "-d" ]]; then
  #statements
elif [[ $1 = "show" || $1 = "-s" ]]; then
  #statements
elif [[ $1 = "clear" || $1 = "-cl" ]]; then
  #statements
elif [[ $1 = "help" || $1 = "-h" ]]; then
  #statements
else
  showHelp
fi

So what's really wrong here? Shouldn't this be valid?

Upvotes: 1

Views: 2863

Answers (1)

Abhishta Gatya
Abhishta Gatya

Reputation: 923

No, bash thinks this is invalid. Turns out you cant have empty clauses as the error was really pointing to the first if statement because I didn't actually put anything inside it, just a placeholder.

I then checked ShellCheck.net to see what's really going on and here it is : shellcheck

To fix this, simply just put any block of code as the statements as the placeholder comments are invalid.

#!/bin/bash

if [[ $1 = "add" || $1 = "-a" ]]; then
  echo add
elif [[ $1 = "done" || $1 = "-d" ]]; then
  echo done
elif [[ $1 = "show" || $1 = "-s" ]]; then
  echo show
elif [[ $1 = "clear" || $1 = "-cl" ]]; then
  echo clear
elif [[ $1 = "help" || $1 = "-h" ]]; then
  showHelp
else
  showHelp
fi

Upvotes: 3

Related Questions