简体   繁体   English

Linux程序(例如bash或python脚本)如何知道它是如何启动的:从命令行还是交互式GUI?

[英]How can Linux program, e.g. bash or python script, know how it was started: from command line or interactive GUI?

I want to do the following: 我想做以下事情:

If the bash/python script is launched from a terminal, it shall do something such as printing an error message text. 如果从终端启动bash / python脚本,它将执行诸如打印错误消息文本之类的操作。 If the script is launched from GUI session like double-clicking from a file browser, it shall do something else, eg display a GUI message box. 如果从GUI会话启动脚本,例如双击文件浏览器,它将执行其他操作,例如显示GUI消息框。

You can check to see whether stdin and stdout are connected to a terminal or not. 您可以检查stdinstdout是否连接到终端。 When run from a GUI, generally stdin is not connected at all, and stdout is connected to a log file. 从GUI运行时,通常stdin完全没有连接, stdout连接到日志文件。 When run from a terminal, both stdin and stdout will be connected to a terminal. 从终端运行时, stdinstdout都将连接到终端。

In Python: 在Python中:

import os
import sys

if os.isatty(sys.stdout.fileno()):
    # print error message text
else:
    # display GUI message

You should check that this will work for you, though, since it doesn't do precisely what you asked for. 但是,您应该检查这是否适合您,因为它不能完全满足您的要求。 But it's the best thing that I can think of that doesn't depend on too much magic. 但这是我能想到的最好的事情,它不依赖于太多的魔法。

You should check that the DISPLAY environment variable is set before going with GUI code too, since it won't work without that. 在使用GUI代码之前,您应该检查DISPLAY环境变量是否已设置,因为没有它,它将无法工作。

Note that terminal users can still redirect stdin or stdout to /dev/null (for example) and this might cause your program to go with the GUI behaviour. 请注意,终端用户仍然可以将stdinstdout重定向到/dev/null (例如),这可能会导致程序使用GUI行为。 So it's far from perfect. 所以它远非完美。

Finally, even though I've given you an answer, please don't do this! 最后,即使我已经给你答案,请不要这样做! It is confusing to users for a program's behaviour to change depending on how it was called. 用户对程序的行为根据其调用方式进行更改会让人感到困惑。

It can check the value of $DISPLAY to see whether or not it's running under X11, and $(tty) to see whether it's running on an interactive terminal. 它可以检查$DISPLAY的值以查看它是否在X11下运行,以及$(tty)以查看它是否在交互式终端上运行。 if [[ $DISPLAY ]] && ! tty; then if [[ $DISPLAY ]] && ! tty; then chances are good you'd want to display a GUI popup. if [[ $DISPLAY ]] && ! tty; then很有可能你想要显示一个GUI弹出窗口。

In the .desktop file that is the menu-entry in gnome/kde/whatever, add a parameter such as 在.desktop文件中,这是gnome / kde / whatever中的菜单项,添加一个参数,如

yourcommand --gui

so the program will be able to know. 所以该计划将能够知道。

Following on to LtWorf's answer (I couldn't describe all this in a comment) 关于LtWorf的回答(我无法在评论中描述所有这些)


Rather than, or in addition to, a parameter, you can create a shell script then create a second link to it with a different name, say somprog and gsomeprog where "gsomeprog" is equivalent to "someprog -gui" 您可以创建一个shell脚本,然后使用不同的名称创建第二个链接,而不是参数,或者添加参数,例如somproggsomeprog ,其中“gsomeprog”等同于“someprog -gui”

$ mv mydevdir/someprog /usr/local/bin
$ cd /usr/local/bin
$ ln someprog gsomeprog

I prefer hard links to ln -s in this case because the "two" programs will always sit next to each other, and will never leave a dangling soft link. 在这种情况下,我更喜欢与ln -s硬链接,因为“两个”程序将始终彼此相邻,并且永远不会留下悬空的软链接。

In the shell script, check the name that was used to invoke it by inspecting arg zero 在shell脚本中,通过检查arg zero来检查用于调用它的名称

#!/bin/sh

mode=console

if [ $(basename ${0}) = gst -o "${1}" = "-gui" ]; then
    mode=gui
fi

echo "Mode is ${mode}"

There is, of course, better option processing than "${1}" = "-gui" available, but that is left as an exercise for the reader. 当然,有比"${1}" = "-gui"更好的选项处理,但这仍然是读者的练习。


I like Robie Basak's answer, but be aware of his someprog > /dev/null caveat. 我喜欢Robie Basak的回答,但要注意他的someprog > /dev/null警告。

You could also run some X11 utility like xdpyinfo ; 您还可以运行一些X11实用程序,如xdpyinfo ; if it runs correctly, you have an X11 server so you are in GUI mode, eg in bash 如果它运行正常,你有一个X11服务器,所以你处于GUI模式,例如在bash中

if xdpyinfo | grep X.Org > /dev/null ; then

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从环境中接收数据,例如bash - How to receive data from the environment e.g. bash 如何使用python的交互式输入运行命令行程序? - How to run a command line program with interactive inputs from python? 如何将GUI样机导出到Python GUI代码(例如wxpython)? - How do I export GUI mockups to Python GUI code (e.g. wxpython)? 在tkinter(python 3)中,如何使程序在继续之前等待事件(例如,单击按钮)? - In tkinter (python 3) how to make the program wait for an event (e.g. button click) before continuing? 如何使用./ 通过 Python 脚本执行命令行程序? - How can I execute command line program with ./ via a Python script? Bash 脚本可以从命令行调用,但不能从 Python 脚本调用 - Bash script can be called from command line, but not from Python script 从脚本(a.py)导入python模块/站点包(例如scipy),但不能从tkinter button命令导入 - Importing a python module/site-package (e.g. scipy) works from a script (a.py), but not from tkinter button command 如何在 python 脚本的终端中获取存储在文件(例如 *.inp)中的多个“用户输入” - How to take multiple "user input" stored in a file (e.g., *.inp) in terminal for python script 如何为ARM交叉编译python包(例如Numpy) - how to cross compile python packages (e.g. Numpy) for ARM Python:如何创建一个函数? 例如 f(x) = ax^2 - Python: How to create a function? e.g. f(x) = ax^2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM