在被另一个bash脚本调用后,获取文件的当前目录 [英] Get current directory of file after getting called by another bash script

查看:141
本文介绍了在被另一个bash脚本调用后,获取文件的当前目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个bash脚本,它调用了另一个bash脚本. 第二个脚本位于另一个文件夹中.

So I have one bash script which calls another bash script. The second script is in a different folder.

script1.sh:
"some_other_folder/script2.sh"
# do something

script2.sh:
src=$(pwd) # THIS returns current directory of script1.sh...
# do something

在第二个脚本中,它的行是src=$(pwd),由于我是从另一个目录中的另一个脚本中调用该脚本,所以$(pwd)返回第一个脚本的当前目录.

In this second script it has the line src=$(pwd) and since I'm calling that script from another script in a different directory, the $(pwd) returns the current directory of the first script.

有什么方法可以在脚本中使用简单的命令来获取第二个脚本的当前目录,而不必传递参数?

Is there any way to get the current directory of the second script using a simple command within that script without having to pass a parameter?

谢谢.

推荐答案

请尝试执行此操作以查看是否有帮助

Please try this to see if it helps

loc=`dirname $BASH_SOURCE`

这篇关于在被另一个bash脚本调用后,获取文件的当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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