简体   繁体   中英

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. 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.

You can check to see whether stdin and stdout are connected to a terminal or not. When run from a GUI, generally stdin is not connected at all, and stdout is connected to a log file. When run from a terminal, both stdin and stdout will be connected to a terminal.

In 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.

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. 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. if [[ $DISPLAY ]] && ! tty; then if [[ $DISPLAY ]] && ! tty; then chances are good you'd want to display a GUI popup.

In the .desktop file that is the menu-entry in gnome/kde/whatever, add a parameter such as

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)


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"

$ 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.

In the shell script, check the name that was used to invoke it by inspecting 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.


I like Robie Basak's answer, but be aware of his someprog > /dev/null caveat.

You could also run some X11 utility like xdpyinfo ; if it runs correctly, you have an X11 server so you are in GUI mode, eg in bash

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM