John
John

Reputation: 887

calculating the path of directory relative to another path with the calculated path being in absolute form

I have this directory structure for an application: bin, config, lib.

In bin directory I have a bash script. I want to be able to set variables in the bash script for config and lib directories based on the location of the script file in the bin directory. I can get the directory name for script that is executing by doing:

BIN_DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

My question is: How can I calculate the diectory paths for config and lib directories based on BIN_DIR ?

Thanks.

Upvotes: 1

Views: 465

Answers (2)

vanje
vanje

Reputation: 10383

If you have BIN_DIR then you get the absolute path for your project directory with:

PROJECT_DIR=`readlink -f $BIN_DIR/..`

and then

CONFIG_DIR=$PROJECT_DIR/config
LIB_DIR=$PROJECT_DIR/lib

Upvotes: 2

Eduardo Ivanec
Eduardo Ivanec

Reputation: 11862

You can probably just use this:

cd `dirname $0`/..
BASE_DIR=`pwd`
cd -
BIN_DIR=${BASE_DIR}/bin
CONFIG_DIR=${BASE_DIR}/config
LIB_DIR=${BASE_DIR}/lib

Upvotes: 3

Related Questions