如何在 shell 脚本中引用当前目录?
所以我有这个脚本,它调用同一目录中的另一个脚本:
#! /bin/sh
#Call the other script
./foo.sh
# do something ...
为此,我得到了
./foo.sh: No such file or directory
所以我把它改成:
#! /bin/sh
#Call the other script
foo.sh
# do something ...
但这会调用
foo
脚本,默认情况下,在 PATH 中。这不是我想要的。所以问题是,引用
./
的语法是什么?在 shell 脚本中?
最佳答案
如果两个脚本都在同一个目录中并且你得到了 ./foo.sh: No such file or directory
错误那么最可能的原因是您从与它们所在的目录不同的目录中运行第一个脚本。将以下内容放入您的第一个脚本中,以便调用 foo.sh
无论您从何处调用第一个脚本,都可以正常工作:
my_dir=`dirname $0`
#Call the other script
$my_dir/foo.sh
关于shell - 在 shell 脚本中引用当前目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10905972/