Question

I'm trying to execute this script from the terminal (via sh PreStartupCommands.sh) and I get the following error in my terminal window:

/home/MinecraftServers/Ephona/serverconfig/PreStartupCommands.sh: line 14: syntax error near unexpected token `fi'
/home/MinecraftServers/Ephona/serverconfig/PreStartupCommands.sh: line 14: `fi'

PreStartupCommands.sh

#!/bin/bash
############################################
########### PreStartupCommands #############
############################################
#
### Update Ephona_sky ###
if [ -f '/home/MinecraftServers/Ephona/Ephona_sky_edit/lock.txt' ] ; then
    echo "Lock exists"
else
    echo "Lock Does Not Exist"
    echo "Copying World ..."
    echo "World Copied"
    echo "Creating Lock"
fi

Everything appears fine to me.

  • The spacing around the [ and ] exists
  • The if, then, else, fi tags are all included
  • All quotations are opened and closed
  • Spelling is correct...
Was it helpful?

Solution

Update: The issue turned out to be that the script file had Windows-style line endings (\r\n) rather than what bash - and Unix utilities in general - expect (\n only), as @devnull suspected.


Normally, sh is symlinked to bash on CentOS, so your script SHOULD work fine.

(As an aside: any POSIX-compliant shell should work with the specific sample script in your question, however.
General caveat: As @Scrutinizer points out in a comment, invoking bash as sh implies subtle changes in behavior - see http://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html (*) - though bashisms still work).

Generally, however, if you're invoking a bash script, it's both conceptually clearer and safer to invoke it with bash - given that sh could have been redefined to invoke a different shell.

Of course, you can run chmod +x PreStartupCommands.sh to make your script executable by itself, which makes the problem go away (assuming the problem is related to what executable is processing the script).

(*) Invoking bash as sh on OSX has additional implications (since sh is a separately compiled executable rather than a symlink there), namely that shell option xpg_echo is turned ON by default, and that in the absence of $FCEDIT, ed is used (without checking for $EDITOR, as bash normally does).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top