0

For quite some time now I have been struggling with this little code segment.

#!/bin/bash

testFunction() { 

        if [ -d "/home/$USER/.skandPATH/" ] ; then 
                #Exists. Do nothing. 
        else 
                mkdir /home/$USER/.skandPATH/   #Does not exist. Creates directory 
                addedPathDir=1 
        fi
} 

testFunction 

Every time I run it I get the following message:

./functionInScript.sh: line 7: syntax error near unexpected token `else'
./functionInScript.sh: line 7: `else '

I've searched through dozens of "syntax error near unexpected token bash" and tried dozens of suggestions.

I have run dos2unix (even though I'm using Mint) on the file. I have escaped the dots in ".skandPATH" but that doesn't seem to make a difference. I have tried deleting every whitespace character and tab character. I have tried commenting out lines and executed the script in order to try and pinpoint the problem, to no avail. I'm at my wits end trying to find what's wrong here!

If you could help me... I would be most grateful.

  • @Ignacio Vazquez-Abrams It worked. I had no idea that it was a must to have at least one command in a block. I am very grateful. Thank you! – user2495181 Jun 26 '13 at 00:49
  • 1
    In the future, you might find [shellcheck.net](http://www.shellcheck.net/) helpful for figuring out such strange bash errors. In this case, it says "Can't have empty then clauses (use 'true' as a no-op)." – that other guy Jun 26 '13 at 01:06
  • @thatotherguy Thanks for the reference. Looks really helpful. – jaypal singh Jun 26 '13 at 01:17
  • @thatotherguy Thank you. I never would've guessed that such a website existed. – user2495181 Jun 26 '13 at 23:28

2 Answers2

5

You must have at least one command in a block; try :.

Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

Why not:

#!/bin/bash

testFunction() { 
    if ! [ -d "/home/$USER/.skandPATH/" ] ; then 
            mkdir /home/$USER/.skandPATH/
            addedPathDir=1
    fi
} 

testFunction 
Rudy Matela
  • 6,310
  • 2
  • 32
  • 37