How to tell the shell program is invoked from GUI environment?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to tell the shell program is invoked from GUI environment?
Hi,
I have some important text info going to standard output.
I want to try my best to have the user see it.
Here is my problem.
If I know the user is using the program (let's say a shell program) in an X11 environment, I could make sure the text is out by using xterm -e <exe name>.
The problem is how do I know xterm is runnable - it needs an X or similar GUI environent.
An idea is to try first to run xterm and to check the exit value of the program. If it returns 0 then everything was fine and the results were printed using xterm. If not the try using just a shell in the console. If you specify language I can help you find the appropriate functions to use.
One question thou...
If xterm -e <cmd> fails, how do I know it is because of xterm failed (in this case I just run cmd without xterm) or cmd failed (I should not retry cmd anymore)
Well... the return value to the script that runs xterm would be the return value of xterm. That means that if $? is not equal to zero, then xterm did not execute correctly. One thing should be aware of is that if the cmd of 'xterm -e cmd' fails to execute, the error will be printed in the xterm that has opened... An idea of making error checking to that file is to not execute the command directly. Execute a script that will execute cmd and will check if it exited without problems. e.g.
#!/bin/bash
#matrying script
cmd
if [ $? != 0 ]; then touch cmd.error;fi
Then you can check from the first script if cmd.error exists by an 'if [ -e cmd.error ]; then...'. You can even logout from the xterm that the cmd is executed if you add an exit command at the end of matrying script and you can execute it with 'xterm -hold -e matrying'. Don't forget to give permission to execute matrying. Hope that helps!
Whoa! You keep making me think! This is tiring you know ;-P
The thing is dorward that you never know when there will be a strange program to use the display environment variable. It is indeed clever to use the DISPLAY, but the above mentioned way would not fail even in extreme situations that DISPLAY is set though you have no X server up. The choice is on oldbee. Another variable he can use is that of TERM. When in X-windows it gets the value xterm usually. But that is usually and not always...
dorward's suggestion to use the DISPLAY is probably best I think. That's how this test is most often done.
Using xterm to test, assumes that the user has a working xterm installed, and xterm is in their path, and that they have permission to run it. It also has the overhead of starting an xterm session.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.