获得bash脚本调用程序脚本的名称 [英] get the name of the caller script in bash script
问题描述
让我们假设我有3个shell脚本
let's assume I have 3 shell scripts
script_1.sh
#!/bin/bash
./script_3.sh
script_2.sh
#!/bin/bash
./script_3.sh
问题是,在 script_3.sh
我想知道来电者脚本的名称。
the problem is that in script_3.sh
I want to know the name of the caller script.
这样我可以给每个来电者,我支持不同的反应 的
的请不要以为我想咨询一下 $ 1,0
事业 $ 1,0
将回声 script_3
不管每次是谁来电的
please don't assume I'm asking about $0
cause $0
will echo script_3
every time no matter who is the caller
下面是预计的输出实例输入
here is an example input with expected output
-
./ script_1.sh
应该呼应script_1
./ script_2.sh
应该呼应 script_2
./ script_3.sh
应该呼应 USER_NAME或根或任何3例区分
这可能吗?如果可能的话,怎么能做到呢?
Is that possible? and If possible, how can it be done?
这将被添加到 RM
修改后的脚本...所以当我打电话 RM
它做一些事情,当混帐
或任何其他CLI工具使用 RM
它不受修改
this is going to be added to a rm
modified script... so when I call rm
it do something and when git
or any other CLI tool use rm
it is not affected by the modification
推荐答案
根据@ user3100381的回答,这里有一个更简单的命令来获取,我相信应该是相当便携同样的事情:
Based on @user3100381's answer, here's a much simpler command to get the same thing which I believe should be fairly portable:
PARENT_COMMAND="$(ps -o comm= $PPID)"
替换 COMM =
与 ARGS =
来得到完整的命令行(命令+参数)。在 =
单独用于燮preSS头。
Replace comm=
with args=
to get the full command line (command + arguments). The =
alone is used to suppress the headers.
请参阅:<一href=\"http://pubs.opengroup.org/onlinepubs/009604499/utilities/ps.html\">http://pubs.opengroup.org/onlinepubs/009604499/utilities/ps.html
这篇关于获得bash脚本调用程序脚本的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!