[英]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. 您可以检查
stdin
和stdout
是否连接到终端。 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. 从终端运行时,
stdin
和stdout
都将连接到终端。
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. 请注意,终端用户仍然可以将
stdin
或stdout
重定向到/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脚本,然后使用不同的名称创建第二个链接,而不是参数,或者添加参数,例如
somprog
和gsomeprog
,其中“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.