Results 1 to 8 of 8

Thread: SH SCRIPT Syntax error: "else" unexpected (expecting "then")

  1. #1
    Join Date
    Oct 2009
    Beans
    17

    Angry SH SCRIPT Syntax error: "else" unexpected (expecting "then")

    BTW, yes I did search for a solution but the only one I found said this error happened if I had an if statement on the same lines as "then".
    Can you guys help me out?
    Here is a part of my script,

    Code:
    if [ -f file1.jar ]; 
        then 
    echo "Found: file 1.jar" 
        else  
    echo "file1.jar: Missing " 
        Down  
    
      if [ -f file2.zip ]; 
       then 
    echo "Found: file2.zip" 
       else  
    echo "file2.zip: Missing " 
        Down  
    
      if [ -f file3.sh ]; 
       then 
    echo Found: file3.sh  
       else   
    echo file.sh: Missing  
        Down
    There alot more and "Down" was defined as a function

    Down() (
    echo "my script is a type of installing script if you needed to know,"
    echo "it Identifies files if there present, if not downloads them and moves them into a java game."
    echo "I get a syntaz error on line 22 which is the else after found: file 1 echo"
    echo "I also have #!/bin/sh at the top, I didnt copy it in"
    )

    Can you help me out?
    Last edited by adrenalin6; March 9th, 2011 at 01:28 PM. Reason: Removed Spaces from previously tried fix

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: SH SCRIPT Syntax error: "else" unexpected (expecting "then")

    use indentation because your script is unreadable and why all these empty lines?

    if syntax goes like this
    Code:
    if <condition>; then
      <stuff>
    else
      <stuff>
    fi
    or
    Code:
    if <condition>
    then
      <stuff>
    else
      <stuff>
    fi
    simply put newline and ; are synonymous in some cases.
    Last edited by Vaphell; March 9th, 2011 at 01:08 PM.

  3. #3
    Join Date
    Oct 2010
    Beans
    659

    Re: SH SCRIPT Syntax error: "else" unexpected (expecting "then")

    you have to terminate a particular if with a corresponding fi..

  4. #4
    Join Date
    Oct 2009
    Beans
    17

    Re: SH SCRIPT Syntax error: "else" unexpected (expecting "then")

    Quote Originally Posted by Vaphell View Post
    use indentation because your script is unreadable and why all these empty lines?

    if syntax goes like this
    Code:
    if <condition>; then
      <stuff>
    else
      <stuff>
    fi
    or
    Code:
    if <condition>
    then
      <stuff>
    else
      <stuff>
    fi
    simply put newline and ; are synonymous in some cases.
    I read somewhere to add lines because its what caused my error, sorry if it made it un readable.
    No reason for double posting, I tried it on the part of my script that was having problems, sorry but same error
    "./installer.sh: 22: Syntax error: "else" unexpected (expecting "then")"
    Last edited by adrenalin6; March 9th, 2011 at 01:25 PM.

  5. #5
    Join Date
    Oct 2009
    Beans
    17

    Re: SH SCRIPT Syntax error: "else" unexpected (expecting "then")

    Quote Originally Posted by neo_aryan View Post
    you have to terminate a particular if with a corresponding fi..
    Tell me if I understood

    Code:
    if < -f file1.sh >;
       then
    <echo "File1">
       else
    <echo "Failed">
        fi
    I read about fi but I concluded it acted like return 0; in C++ (for example)
    So it's to finish an "if"?
    Also Same Problem

    Error Changed after doing some editing,
    its " ./installer.sh: 24: Syntax error: "else" unexpected (expecting "}") "

    the from the start to line 24 is

    Code:
    #!/bin/sh
    
    pause()
    {    read -n1 -t5 any_keyread -n1 -t5 any_key
        echo 
    }
    
    Start()
    {
     
    echo "Starting File Check"
    pause
     
    if [ -f crackedclient.jar ]; 
    
    then 
        {
    echo "Found: crackedclient.jar"
    } 
    else {
    echo "crackedclient.jar: Missing " 
        Down }
    fi
    I cleaned up the useless echo lines at the top but its all the way from start to line 23.
    (I will be AFK but I will be back in 7 hours)
    Last edited by adrenalin6; March 9th, 2011 at 01:53 PM. Reason: I may of got it wrong but I'm receiving the same syntax error on the same line.

  6. #6
    Join Date
    Oct 2010
    Beans
    659

    Re: SH SCRIPT Syntax error: "else" unexpected (expecting "then")

    though i don know much but it is something like opening and closing braces in c++ so if you have if you should have a corresponding fi so that computer may understand which if ends here or something like that..go for a standard guide for better explanation but i think that fi is the problem for you

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: SH SCRIPT Syntax error: "else" unexpected (expecting "then")

    don't put < > there, i merely used them to indicate customizable parts of the expression , use real conditions in [] and normal commands. And with valid script you don't need to put an empty line between every 2 lines of code. Below example works just fine

    Code:
    #!/bin/bash
    
    if [ -f file ]
    then
      echo line1
      echo line2
    else
      echo line3
    fi
    Last edited by Vaphell; March 9th, 2011 at 02:00 PM.

  8. #8
    Join Date
    Oct 2009
    Beans
    17

    Smile Re: SH SCRIPT Syntax error: "else" unexpected (expecting "then")

    Thanks guys for help,
    but I epicly failed, I had a pause function that didnt work but didnt know.
    Now the script deleted the game files, folders and it self including all copys of itself.

    Moral is: DONT KEEP BACKUPS IN SAME FOLDER WITH A SCRIPT CONTAINING rm *.
    lol

    -Henry
    Last edited by adrenalin6; March 9th, 2011 at 10:08 PM. Reason: Added Humor (English not mother tongue)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •