shell - 在 shell 脚本中引用当前目录

标签 shell

如何在 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/

相关文章:

java - 无法将值从 Bash 脚本传递到 Java 程序

windows - 批处理 : parameter with both expansion symbols

c# - C++ Client 和 C# Server 通过 Network Stream 通信

linux - 带反引号的远程 shell 命令的退出状态

php - 执行shell命令在后台运行php

linux - 遍历进程树

带有可与管道和 glob 配合使用的参数的 Bash 别名

linux - 如何检查 shell 脚本中 if 语句中的两种可能性?

linux - 如何在此 shell 脚本上添加字符串名称?

linux - Linux,如何使用Shell脚本进行文件操作?