1

I have a directory structure of package/bin and package/lib, where package/lib contains jar files and package/bin should contain some scripts.

I am interested in writing scripts in package/bin that basically run java classes from lib/. These scripts should be runnable from any directory, not necessarily package/, package/bin/ or package/lib.

This means that the script in package/bin, let's call it 'run.sh' should do something like:

  1. identify the directory from which run.sh script is running.
  2. strip off the directory so that we end up with package/
  3. add bin/ to it
  4. run the necessary java classes with this deduced directory.

What would be the best way to follow steps 1-3? I would like it to be almost platform-independent, so that at the very least it would work on all Unix systems and OSX. I want to avoid using perl (I could easily write this in perl), and instead opt for a shell script.

Thanks.

kloop
  • 3,617
  • 8
  • 37
  • 57
  • see http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in to get past steps 1 & 2 — if you're on a system with Gnu Bash, at least. (Put #!/bin/bash ) — not as portable to other POSIX Bourne shells, though, so might limit you to OSX and Linux, but not necessarily all Unix (POSIX) systems. – BRPocock Dec 05 '11 at 19:43

1 Answers1

0

Along the BASH lines....

my_package_path=$(dirname `dirname $0` )

$0 gives you the path to the script you are running dirname $0 gives you the path to the folder your script is in

lonestar21
  • 933
  • 2
  • 10
  • 22