Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5999793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:36:08+00:00 2026-05-23T00:36:08+00:00

How can I get the directory a bash script file is in, when that

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:36:09+00:00Added an answer on May 23, 2026 at 12:36 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can you get the directory of the script that was run and use
I need to solve the following question which i can't get to work by
I have a bash script which calls another bash script, like so: #!/bin/bash echo
We have a custom script that we can run by redirecting a temporary file
Can someone help me check my bash script? i'm trying to feed a directory
How can I get a full list of Groups in my Active Directory?
How do you get a list of files within a directory so each can
I can get easily see what projects and dlls a single project references from
Logging can get complicated, quickly. Considering that you have some code, how do you
I can get the executable location from the process, how do I get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.