检查bash脚本从shell或其他脚本/应用程序调用 [英] Check if bash script was invoked from a shell or another script/application

查看:183
本文介绍了检查bash脚本从shell或其他脚本/应用程序调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个bash脚本从另一个命令输出重定向到正确的位置。基本上,当脚本从shell /命令行调用我想输出发送到stdout。但是,当bash脚本从其它应用程序(例如,另一个bash脚本,一些应用程序,或在我的情况下,从真棒提示符插件在我真棒窗口管理器)我要输出重定向别处执行。

I am writing a bash script to redirect output from another command to the proper location. Basically, when the script is invoked from a shell/commandline I want to send the output to STDOUT. But, when the bash script is executed from some other application (e.g. another bash script, some application, or in my case from the awesome-prompt plugin in my Awesome Window Manager) I want to redirect the output somewhere else.

有没有在bash任何方式看到脚本是如何调用?

Is there any way in bash to see how a script was invoked?

推荐答案

试试这个:

ps -o stat= -p $PPID

如果结果中包含的s(小写),它是通过命令行运行或在脚本中后台运行。告诉这两个相距:

If the result contains "s" (lowercase) it was either run from the command line or backgrounded from within a script. To tell those two apart:

ps -o stat= -p $$

将包含一个+,如果它没有后台运行

will contain a "+" if it was not backgrounded.

下面的表格:

Run          $$    $PPID
CL           S+    Ss
CL&          S     Ss+
Script       S+    S+
Script&      S     S
Script(&)    S     Ss
Script&(&)   S     NULL

在哪里(安培)是指孩子剧本后台运行,并与放大器;是指父脚本(这是脚本是指)跑它后台运行。 CL表示命令行。 NULL表示ps输出空和 $ PPID 为1。

男子PS

   s    is a session leader
   +    is in the foreground process group

应该指出的是,这个答案是基于GNU PS ,但该男子页BSD(包括OS X)表示了类似的功能。和GNU PS 是一个混血儿,包括BSD的功能,等等。

It should be noted that this answer is based on GNU ps, but the man pages for BSD (including OS X) indicate similar functionality. And GNU ps is a hybrid that includes BSD functionality, among others.

这篇关于检查bash脚本从shell或其他脚本/应用程序调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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