Find the answer to your Linux question:
Results 1 to 9 of 9
My bash script includes the following: Code: # Read config file if it is present. if [ -r /etc/default/$NAME ] then . /etc/default/$NAME fi if [ "$STARTUP" = "no" ]; ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    19

    Unhappy Script syntax error but why?

    My bash script includes the following:
    Code:
    # Read config file if it is present.
    if [ -r /etc/default/$NAME ]
    then
    	. /etc/default/$NAME
    fi
    
    if [ "$STARTUP" = "no" ]; then
    	echo "Need to configure config.xml before starting the server"
    	exit 0
    fi
    I get a syntax error when I run it
    /etc/init.d/red5: 32: Syntax error: "fi" unexpected (expecting "then")

    where line 32 is the last line with 'fi'
    Last edited by devils casper; 04-22-2008 at 12:17 PM. Reason: Added [code]...[/code] tag.

  2. #2
    Just Joined!
    Join Date
    Dec 2006
    Posts
    52
    I think you will find that if you remove "; then" from the second if statement and put "then" on its own line then you will be fine. I can't remember the details as to why that is true but what you have won't work.

  3. #3
    Linux Engineer aliov's Avatar
    Join Date
    Dec 2006
    Location
    Geneva,Beirut
    Posts
    1,078
    Quote Originally Posted by davidsc View Post
    My bash script includes the following:
    Code:
    # Read config file if it is present.
    if [ -r /etc/default/$NAME ] then
    you forgot the semi column.
    Linux is not only an operating system, it's a philosophy.
    Archost.

  4. #4
    Just Joined!
    Join Date
    Feb 2008
    Posts
    19
    The original script had the first if fo construct which is fine.
    The second one I have introduced to check a variable but it is causing the syntax error. Taking the ; away and putting 'then' on the next line doesn't get rid of the syntax error.

  5. #5
    Just Joined! mitchpotter's Avatar
    Join Date
    Jan 2008
    Location
    Orlando
    Posts
    19
    The semicolon syntax is correct if you want to have then on the same line as the if statement. I've seen similar errors when if statements outside/around or above the code had mismatched if:then:fi problems. You might want to look at other tests in your code.

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by mitchpotter View Post
    The semicolon syntax is correct if you want to have then on the same line as the if statement. I've seen similar errors when if statements outside/around or above the code had mismatched if:then:fi problems. You might want to look at other tests in your code.
    Yep. But I would personally start by correctly quoting all the variables. It's very likely that some weird string is causing all the trouble.

  7. #7
    Just Joined!
    Join Date
    Apr 2008
    Posts
    35
    I cut and pasted your original code and didn't get an error.

    It's possible that you have some invisible control characters in your script. Did you ftp it from one place to another (possible ascii/binary problem)? You should be able to see if this is the issue by doing

    cat -v yourScript

    If it is, the easiest way to fix this is to run dos2unix, or you can edit the file with vi and just replace the control chars with nothing

    Hope that helps

    , Mike

  8. #8
    Just Joined!
    Join Date
    Feb 2008
    Posts
    19

    Wink Spot on!

    Insidious little error.

  9. #9
    Just Joined!
    Join Date
    Apr 2008
    Posts
    35
    Good news Glad I could help you out

    Best wishes,

    Mike

Posting Permissions

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