我该如何执行一个任意脚本,其目录的工作目录? [英] How do I execute an arbitrary script with a working directory of the directory its in?

查看:110
本文介绍了我该如何执行一个任意脚本,其目录的工作目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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脚本,我这样做:

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

/opt/script/myscript.groovy &

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

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

可能你是从 /etc/init.d

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

OR

...为保持动态,请添加:
cd$(dirname$ 0)

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

这篇关于我该如何执行一个任意脚本,其目录的工作目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆