I need to execute a groovy script file from bash, and I need the script to have a working directory of the directory it exists in.
That is, in my bash script, I'm doing this:
/opt/script/myscript.groovy &
But this seems to set the working directory to /etc/init.d
, the directory I'm calling from. How do I change the working directory for that script to /opt/script
?
In
bash
putting that in the script works best:This will succeed even with the following invocation (of
/path/to/script.sh
):where
HERE=$(dirname $0)
would fail.Optionally you could also use
pwd -P
instead of justpwd
, then$HERE
will contain the realpath (canonicalized absolute pathname) as ofman 3 realpath
.Something like this maybe:
probably you are runnig (starting) that script from
/etc/init.d
?Add
cd /opt/script
at the first line of the scriptOR
...to keep it dynamic, add:
cd "$(dirname "$0")"
If you are using start-stop-daemon inside your /etc/init.d script, you can take advantage of the -d parameter for achieving this: