C
C
cypok2011-07-27 16:56:04
linux
cypok, 2011-07-27 16:56:04

A small question about the sh-script?

There is some script vars.sh :
export PATH=`pwd`/bin:$PATH
Working with it looks like this:

$ cd /some/working/dir<br/>
$ great_app args<br/>
&nbsp;&nbsp;error: not found!<br/>
$ cd /some/interesting/dir<br/>
$ ls bin<br/>
&nbsp;&nbsp;great_app<br/>
$ . vars.sh<br/>
$ cd /some/working/dir<br/>
$ great_app args<br/>
&nbsp;&nbsp;i'm working!<br/>

Since vars.sh uses pwd, it needs to be in the same folder as the script itself to run it.
And I want to run vars.sh from any folder, leaving the entire package ( vars.sh script and bin folder ) easily moved from one folder to another, in short, without absolute paths.
As a result, you want this:
$ cd /some/working/dir<br/>
$ . /some/interesting/dir/vars.sh<br/>
$ great_app args<br/>
&nbsp;&nbsp;i'm working!<br/>

My attempts did not lead to anything good, does anyone know how this can be done?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
Ivan, 2011-07-27
@iSage

If I understood correctly:
export PATH=`dirname $0`/bin:$PATH

G
Gribozavr, 2011-07-27
@gribozavr

Better simpler - make yourself an alias or a function in ~/.bashrc.

O
ob1, 2011-07-27
@ob1

Unfortunately, I did not fully understand the author. Would it be enough to add ./bin to PATNA?

D
danfe, 2011-07-28
@danfe

I do this: at the beginning of the script, I set up the MY_DIR variable:
MY_DIR=`readlink -f "$(dirname "$0")"`
Now it will contain the full path to the script, regardless of where it was launched from. Run other scripts already relative to this directory. I think this is preferable to modifying PATH (albeit temporarily).
Yes, please note: all quotes are required.

3
3vi1_0n3, 2014-12-19
@3vi1_0n3

What an old horror question, nevertheless I will offer my own version:

SCRIPT_NAME=${0##*/}
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
FULL_SCRIPT_NAME=${SCRIPT_DIR}/${SCRIPT_NAME}
echo ${FULL_SCRIPT_NAME}

You can thus get the full path to the directory where the script was launched from, as well as the full name of the script with the path.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question