ln -s creates symlink in symlink, if [ -f ... ] says file that exists doesn't exist

Hi Forums,

I got a little problem, I made a few modifications to the code of the launch script of a testing server(minecraft) and now updating is broken aswell as the automatic directory creation.

These Lines somehow create an endless symlink that refers to itself and I don't know how to fix that:

        for INDEX in ${!WORLDNAME[@]}
        do
                if [ ! -d "$BACKUPPATH/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir $BACKUPPATH/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -d "$WORLDSTORAGE/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir $WORLDSTORAGE/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -d "$RAMDISK/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir RAMDISK/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -L "$MCPATH/${WORLDNAME[$INDEX]}" ]; then
                        as_user "ln -s $RAMDISK/${WORLDNAME[$INDEX]} $MCPATH/${WORLDNAME[$INDEX]}"
                fi
        done

Also if [ -f ... ] reports that a file that was just downloaded doesn't exist:

                as_user "wget -O $MCPATH/craftbukkit.jar.update $DOWNLOADURL"
                if [ -f "$MCPATH/craftbukkit.jar.update" ]; then

This has bothered me for the past 3 weeks but I couldn't come up with a solution or even an answer to why it is doing it, so I guess I need help :confused:

The full Script:

#!/bin/bash
# /etc/init.d/minecraft

### BEGIN INIT INFO
# Provides:   minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop:  $local_fs $remote_fs
# Should-Start:   $network
# Should-Stop:    $network
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:    Minecraft server
# Description:    Init script for minecraft/bukkit server, with rolling logs and use of ramdisk for less lag.
### END INIT INFO

#############################
######### SETTINGS ##########
#############################
# SERVICE       - Name of the server software jar file
# SCREEN        - Name to use for the screen instance
# USERNAME      - User that we should run the server under
# MCPATH        - Path to the server software's root
# CPU_COUNT     - Count of CPUs/Cores to use for Java
# INITMEM       - Initial memory to take up in RAM (does not include ramdisk)
# MAXMEM        - Maximal memory to take up in RAM (does not include ramdisk)
# INVOCETION    - The command used to start the server software
# BACKUPPATH    - Where should we store per-world backups?
# LOGPATH       - Where should log-roll's be stored to?
# WHOLEBACKUP   - Where should we complete server backups to?
# WORLDSTORAGE  - Where should the physical world copies be at?
# WORLDNAME[@]  - How are all the Worlds named? (Replace @ with an increasing number)
# WORLDRAM[@]   - Should the world @ be stored on the ramdisk? (Replace @ with World number, see WORLDNAME)
# RAMDISK       - Where is the ramdisk located?
# KEEPBACKUPTIME- How old is the last backup allowed to be? (anything past this amount of days gets deleted)
# RETRYSTOP     - How often should we check for the server to be shut down after we told it to 'stop'?
# RETRYWAIT     - How much time lies between each check?
#################
MCPATH="~/server"
BACKUPPATH="~/worldBackup"
LOGPATH="~/logBackup"
WHOLEBACKUP="~/serverBackup"
WORLDSTORAGE="~/worldStorage"
RAMDISK="~/ramdisk/host1"
SCREEN="host1-server"
SERVICE="craftbukkit.jar"
USERNAME="host1"
CPU_COUNT=3
INITMEM="128M"
MAXMEM="4096M"
INVOCATION="java -Xmx$MAXMEM -Xms$INITMEM -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar $SERVICE nogui"
WORLDNAME[1]="Normal"
WORLDNAME[2]="Normal_nether"
WORLDNAME[3]="Skylands"
WORLDRAM[1]=true
WORLDRAM[2]=true
WORLDRAM[3]=true
KEEPBACKUPTIME=30
RETRYSTOP=3
RETRYWAIT=5
DOWNLOADURL="http://ci.bukkit.org/job/dev-CraftBukkit/lastStableBuild/artifact/target/craftbukkit-1.0.0-SNAPSHOT.jar"
################

ME=`whoami`
as_user() {
        if [ $ME == $USERNAME ] ; then
                bash -c "$1"
        else
                su - $USERNAME -c "$1"
        fi
}

#Setup folders and links
do_setup_folders() {
        if [ ! -d "$BACKUPPATH" ]; then
                as_user "mkdir $BACKUPPATH"
        fi
        if [ ! -d "$LOGPATH" ]; then
                as_user "mkdir $LOGPATH"
        fi
        if [ ! -d "$WHOLEBACKUP" ]; then
                as_user "mkdir $WHOLEBACKUP"
        fi
        if [ ! -d "$WORLDSTORAGE" ]; then
                as_user "mkdir $WORLDSTORAGE"
        fi
        if [ ! -d "$RAMDISK" ]; then
                as_user "mkdir $RAMDISK"
        fi
        for INDEX in ${!WORLDNAME[@]}
        do
                if [ ! -d "$BACKUPPATH/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir $BACKUPPATH/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -d "$WORLDSTORAGE/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir $WORLDSTORAGE/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -d "$RAMDISK/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir RAMDISK/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -L "$MCPATH/${WORLDNAME[$INDEX]}" ]; then
                        as_user "ln -s $RAMDISK/${WORLDNAME[$INDEX]} $MCPATH/${WORLDNAME[$INDEX]}"
                fi
        done
}

server_running() {
        if ps ax | grep -v grep | grep $SCREEN | grep $USERNAME | grep $SERVICE > /dev/null
        then
                return 0
        else
                return 1
        fi
}

datepath() {
        echo $1`date +%FT%T`$3
}

mc_start() {
        if server_running
        then
                echo "Tried to start but $SERVICE was already running!"
        else
                echo "$SERVICE was not running... starting."
                as_user "cd $MCPATH && screen -dmS $SCREEN $INVOCATION"
                sleep 7
                if server_running
                then
                        echo "$SERVICE is now running."
                else
                        echo "Could not start $SERVICE."
                fi
        fi
}

mc_saveoff() {
        if server_running
        then
                echo "$SERVICE is running... suspending saves"
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-off\"\015'"
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-all\"\015'"
                sleep 10
                sync
        else
                echo "$SERVICE was not running. Not suspending saves."
        fi
}

mc_saveon() {
        if server_running
        then
                echo "$SERVICE is running... re-enabling saves"
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-on\"\015'"
        else
                echo "$SERVICE was not running. Not resuming saves."
        fi
}

mc_stop() {
        if server_running
        then
                echo "$SERVICE is running... stopping."
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-all\"\015'"
                sleep 10
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"stop\"\015'"
                sleep $RETRYWAIT
        else
                echo "$SERVICE was not running."
        fi
        for (( i = 0; i <= $RETRYSTOP; i++ ))
        do
                if server_running
                then
                        echo "Waiting for $SERVICE to shut down, try $i of $RETRYSTOP"
                else
                        echo "$SERVICE stopped."
                        return
                fi
                sleep $RETRYWAIT
        done
}
log_roll() {
        as_user "mkdir -p $LOGPATH"
        path=`datepath $LOGPATH/server_ .log.gz .log`
        as_user "mv $MCPATH/server.log $path && gzip $path"
}
mc_whole_backup() {
        as_user "mkdir -p $WHOLEBACKUP"
        path=`datepath $WHOLEBACKUP/ .tar.bz2 .tar.bz2`
        as_user "tar -hcjf $path $MCPATH"
}
mc_world_backup() {
        as_user "mkdir -p $BACKUPPATH"
        for INDEX in ${!WORLDNAME[@]}
        do
                echo "Backing up minecraft ${WORLDNAME[$INDEX]}"
                oldfile=`ls $BACKUPPATH/${WORLDNAME[$INDEX]} -tr | tail -n 1`
                path=`datepath $BACKUPPATH/${WORLDNAME[$INDEX]}/ .tar.bz2 .tar.bz2`
                as_user "tar -hcjf $path $MCPATH/${WORLDNAME[$INDEX]}"
                if ! `diff '$BACKUPPATH/${WORLDNAME[$INDEX]}/$oldfile' '$path' >/dev/null`
                then
                        as_user "rm -f '$path'"
                fi
                as_user "find '$BACKUPPATH/${WORLDNAME[$INDEX]}' -mtime +$KEEPBACKUPTIME -exec rm -f {} \;"
        done
}
check_links() {
        do_setup_folders
}
to_ram() {
        for INDEX in ${!WORLDNAME[@]}
        do
                if ${WORLDRAM[$INDEX]}
                then
                        if [ -L $MCPATH/${WORLDNAME[$INDEX]} ]
                        then
                                as_user "rsync -rt ${WORLDSTORAGE}/${WORLDNAME[$INDEX]}/ $RAMDISK/${WORLDNAME[$INDEX]}"
                                echo "${WORLDNAME[$INDEX]} copied to ram"
                        fi
                fi
        done
}
to_disk() {
        for INDEX in ${!WORLDNAME[@]}
        do
                as_user "rsync -rt $MCPATH/${WORLDNAME[$INDEX]}/ ${WORLDSTORAGE}/${WORLDNAME[$INDEX]}"
                echo "${WORLDNAME[$INDEX]} copied to disk"
        done
}
mc_update() {
        if server_running
        then
                echo "$SERVICE is running! Will not start update."
        else
                echo "Updating craftbukkit...."
                as_user "wget -O $MCPATH/craftbukkit.jar.update $DOWNLOADURL"
                if [ -f "$MCPATH/craftbukkit.jar.update" ]; then
                        if `diff $MCPATH/$SERVICE $MCPATH/craftbukkit.jar.update > /dev/null`
                        then
                                echo "You are already running the latest version of CraftBukkit."
                                as_user "rm $MCPATH/craftbukkit.jar.update"
                        else
                                as_user "mv $MCPATH/craftbukkit.jar.update $MCPATH/$SERVICE"
                                echo "CraftBukkit successfully updated."
                        fi
                else
                        echo "CraftBukkit update could not be downloaded."
                fi
        fi
}

case "$1" in
        start)
                # Starts the server
                check_links
                to_ram
                mc_start
                ;;
        stop)
                # Stops the server
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say SERVER SHUTTING DOWN!\"\015'"
                mc_stop
                to_disk
                ;;
        restart)
                # Restarts the server
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say SERVER REBOOT IN 10 SECONDS.\"\015'"
                mc_stop
                mc_start
                ;;
        backup)
                # Backups world
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say Backing up world.\"\015'"
                mc_saveoff
                to_disk
                mc_world_backup
                mc_saveon
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say Backup complete.\"\015'"
                ;;
        whole-backup)
                # Backup everything
                mc_whole_backup
                ;;
        update)
                #update minecraft_server.jar and craftbukkit.jar (thanks karrth)
                as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER UPDATE IN 10 SECONDS.\"\015'"
                mc_stop
                to_disk
                mc_update
                mc_start
                ;;
        to-disk)
                # Writes from the ramdisk to disk, in case the server crashes.
                # Using ramdisk speeds things up a lot, especially if you allow
                # teleportation on the server.
                mc_saveoff
                to_disk
                mc_saveon
                ;;
        connected)
                # Lists connected users
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff list\015'"
                sleep 3s
                tac $MCPATH/server.log | grep -m 1 "Connected"
                ;;
        log-roll)
                # Moves and Gzips the logfile, a big log file slows down the
                # server A LOT (what was notch thinking?)
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say ROUTINE REBOOT IN 10 SECONDS.\"\015'"
                mc_stop
                log_roll
                mc_start
                ;;
        status)
                # Shows server status
                if server_running
                then
                        echo "$SERVICE is running."
                else
                        echo "$SERVICE is not running."
                fi
                ;;
        version)
                echo Craftbukkit version `awk '/Craftbukkit/ {sub(/\)/, ""); print $12}' $MCPATH/server.log`
                ;;
        links)
                check_links
                ;;
        help)
                echo "Usage: /etc/init.d/minecraft command"
                echo
                echo "start - Starts the server"
                echo "stop - stops the server"
                echo "restart - restarts the server"
                echo "backup - backups the worlds defined in the script"
                echo "whole-backup - backups the entire server folder"
                echo "update - fetches the latest version of minecraft.jar server and Bukkit"
                echo "log-roll - Moves and gzips the logfile"
                echo "to-disk - copies the worlds from the ramdisk to worldstorage"
                echo "connected - lists connected users"
                echo "status - Shows server status"
                echo "version - returs Bukkit version"
                echo "links - creates nessesary symlinks"
                ;;
        *)
                echo "No such command see /etc/init.d/minecraft help"
                exit 1
                ;;
esac

exit 0

I appreciate any help you could give me, I'm relatively new to Shell Scripting and this error goes past my previous experience of logical. Thanks in Advance,

Xaymar