Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the directory a bash script file is in

Tags:

bash

shell

How can I get the directory a bash script file is in, when that script file is included from another (which makes it different from this question)?

/script1.sh

. /dir2/script2.sh

/dir2/script2.sh

# echoes "/dir2"
echo whatevergetsthatdir

This is the script I'm trying to "fix"

/etc/init.d/silvercar-gameserver (unique for every instance)

#!/bin/bash
#
#       /etc/rc.d/init.d/silvercar-gameserver
#
#       Initscript for silvercar game server
#
# chkconfig: 2345 20 80
# description: lalalalala

#CONFIG
BIN=/opt/silvercar-gameserver # Want to get rid of this
CONF=/etc/opt/silvercar-gameserver

. /etc/init.d/functions
. $BIN/gameserver.sh.inc

exit 0

/opt/silvercar-gameserver/gameserver.sh.inc (not to be changed for each install. is in svn)

# Meant to be included from a script in init.d
# Required input:
#  CONF (e.g. /etc/opt/silvercarserver)


# -- Installation config (must provide JSVC, JAVA_HOME)
. $BIN/localconf.sh

# -- Instance config (must provide ASUSER, ASWORK)
. $CONF/conf.sh

PIDFILE=$ASWORK/running.pid
LOGDIR=$ASWORK/log
CLASS=tr.silvercar.gameserver.runner.DaemonGameServer
ARGS=$CONF

start() {
    echo "Going to start Gameserver..."

    export JAVA_HOME
    cd $BIN
    $JSVC -jvm server -procname silvercar-gameserver -pidfile $PIDFILE -user $ASUSER -outfile $LOGDIR/stdout -errfile $LOGDIR/stderr \
             -cp `cat classpath` -Dlogroot=$LOGDIR $CLASS $ARGS

    echo "Gameserver started!"
}

stop() {
    echo "Going to stop Gameserver..."
    $JSVC -stop -pidfile $PIDFILE $CLASS
    echo "Gameserver stopped!"
}

case "$1" in
    start)        
        start          
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: /etc/init.d/silvercar-gameserver {start|stop|restart}"
        exit 1
        ;;
esac
like image 282
Bart van Heukelom Avatar asked Jun 09 '11 11:06

Bart van Heukelom


2 Answers

If you have bash 3.0 or newer, you can use BASH_SOURCE to get exactly what you need without relying on the calling program to set any environment variables for you. See below for an example:

#test.sh
source some_directory/script2.sh

#some_directory/script2.sh
echo $BASH_SOURCE
echo $(dirname $BASH_SOURCE)

The output will be:

$ ./test.sh
some_directory/script2.sh
some_directory
like image 183
Steve Prentice Avatar answered Nov 20 '22 21:11

Steve Prentice


In order to source the file, the parent script obviously knows the path where the child script is. Set it as a variable, then in the child script check for that variable. If it is available you know it's been sourced and you can use that path, otherwise use the normal trick in the question you linked.

# script1.sh
RESOURCE_DIR=/dir2
source $RESOURCE_DIR/script2.sh

# script2.sh
if [ -z "$RESOURCE_DIR"]; then
  echo $RESOURCE_DIR
else
  echo $(dirname $0)
fi
like image 5
Caleb Avatar answered Nov 20 '22 19:11

Caleb