linux - 哪个命令找不到可执行的Unix脚本

标签 linux bash unix system-administration

我有一个由前队友编写的 Unix bash 脚本,它必须在我的 PATH 中,但我无法通过手动检查找到它(见下文)。但是,我可以通过键入在任何地方执行它

$ my_script

我想查看和编辑这个脚本。但是,当我尝试通过 which 命令查找它时,我得到的是空白响应。返回码表示错误:

$ which my_script
$ echo $?
1

然而,我可以运行脚本。我手动梳理了我的PATH,没找到。老实说,我已经 20 多年没有遇到过这样的事情了。除了 which 之外,还有其他命令和/或可以隐藏此类脚本的方法吗?

最佳答案

要确定可从 shell 调用的任何命令类型使用 type builtin .

在您的情况下,由于假定的脚本 变成了shell 函数,您会看到(假设bash):

$ type my_script  # Bash: option -t would output just 'function`
my_script is a function
my_script () 
{                
...               # The function's definition

从那里 - 正如您所做的那样 - 您可以检查配置文件(例如 ~/.bash_profile )和/或初始化脚本(例如 ~/.bashrc )以确定函数的来源。

警告:type 输出的函数签名被标准化为 <name> ()形式 - 即使您将函数定义为 function <name>

bash您甚至可以直接找到给定函数的定义位置(感谢 this superuser.com answerCharles Duffy 启发我找到它并提出最简洁的形式):

$ (shopt -s extdebug; declare -F my_func)
my_func 112 /Users/jdoe/.bashrc   # sample output

112是指示的脚本内的行号。

关于linux - 哪个命令找不到可执行的Unix脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35852780/

相关文章:

bash - 如何使用 bash 将充满 IP block 的文件批量添加到 IPTables

objective-c - 用于交互式 Unix 命令的 Cocoa 包装器

c - Linux 驱动程序 : Copy memory from different user-process

java - Android Studio AVD 未检测到 JRE

linux - 'usr/bin' 不包含在路径环境中

python - XmbTextListToTextProperty 结果代码 -2 : error during executing cv2. imshow 来自 shell 脚本的 .py 文件

linux - Linux/Unix 应用程序的源代码最适合保存在哪里?

linux - 使用 cp 将 .html 文件转换为 .txt |错误 : cp: cannot stat '/*.html' : No such file or directory

c++ - 如何处理 Unix 管道

c - 使用 posix_spawn 设置 euid