如何使用其所在目录的工作目录执行任意脚本?

时间:2022-07-18 07:09:50

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.

我需要从bash执行一个groovy脚本文件,我需要脚本有一个它所在目录的工作目录。

That is, in my bash script, I'm doing this:

也就是说,在我的bash脚本中,我这样做:

/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?

但这似乎将工作目录设置为/etc/init.d,即我正在调用的目录。如何将该脚本的工作目录更改为/ opt / script?

4 个解决方案

#1


8  

/etc/init.d

/etc/init.d中

probably you are runnig (starting) that script from /etc/init.d?

可能你是/etc/init.d的runnig(启动)脚本?

Add cd /opt/script at the first line of the script

在脚本的第一行添加cd / opt / script

OR

要么

...to keep it dynamic, add: cd "$(dirname "$0")"

...保持动态,添加:cd“$(dirname”$ 0“)”

#2


15  

If you are using start-stop-daemon inside your /etc/init.d script, you can take advantage of the -d parameter for achieving this:

如果在/etc/init.d脚本中使用start-stop-daemon,则可以利用-d参数来实现此目的:

   -d, --chdir path
          Chdir to path before starting the process. This is done after the chroot if the -r|--chroot option is set. When not specified, start-stop-daemon will chdir to the root directory before starting the process.

#3


2  

In bash putting that in the script works best:

在bash中将其放入脚本中效果最佳:

HERE=$(cd -- $(dirname ${BASH_SOURCE[0]}) > /dev/null && pwd)
cd -- "$HERE"

This will succeed even with the following invocation (of /path/to/script.sh):

即使使用以下调用(/path/to/script.sh),这也会成功:

PATH="/path/to:$PATH" bash script.sh

where HERE=$(dirname $0) would fail.

HERE = $(dirname $ 0)将失败。

Optionally you could also use pwd -P instead of just pwd, then $HERE will contain the realpath (canonicalized absolute pathname) as of man 3 realpath.

您也可以选择使用pwd -P而不仅仅是pwd,然后$ HERE将包含man 3 realpath的realpath(规范化绝对路径名)。

#4


1  

Something like this maybe:

这样的事情可能是:

SCRIPT=/opt/script/myscript.groovy
pushd `dirname $SCRIPT`
./`basename $SCRIPT`
popd

#1


8  

/etc/init.d

/etc/init.d中

probably you are runnig (starting) that script from /etc/init.d?

可能你是/etc/init.d的runnig(启动)脚本?

Add cd /opt/script at the first line of the script

在脚本的第一行添加cd / opt / script

OR

要么

...to keep it dynamic, add: cd "$(dirname "$0")"

...保持动态,添加:cd“$(dirname”$ 0“)”

#2


15  

If you are using start-stop-daemon inside your /etc/init.d script, you can take advantage of the -d parameter for achieving this:

如果在/etc/init.d脚本中使用start-stop-daemon,则可以利用-d参数来实现此目的:

   -d, --chdir path
          Chdir to path before starting the process. This is done after the chroot if the -r|--chroot option is set. When not specified, start-stop-daemon will chdir to the root directory before starting the process.

#3


2  

In bash putting that in the script works best:

在bash中将其放入脚本中效果最佳:

HERE=$(cd -- $(dirname ${BASH_SOURCE[0]}) > /dev/null && pwd)
cd -- "$HERE"

This will succeed even with the following invocation (of /path/to/script.sh):

即使使用以下调用(/path/to/script.sh),这也会成功:

PATH="/path/to:$PATH" bash script.sh

where HERE=$(dirname $0) would fail.

HERE = $(dirname $ 0)将失败。

Optionally you could also use pwd -P instead of just pwd, then $HERE will contain the realpath (canonicalized absolute pathname) as of man 3 realpath.

您也可以选择使用pwd -P而不仅仅是pwd,然后$ HERE将包含man 3 realpath的realpath(规范化绝对路径名)。

#4


1  

Something like this maybe:

这样的事情可能是:

SCRIPT=/opt/script/myscript.groovy
pushd `dirname $SCRIPT`
./`basename $SCRIPT`
popd