LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2002, 10:29 AM   #1
oldbee
LQ Newbie
 
Registered: Jan 2002
Posts: 20

Rep: Reputation: 0
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.

Thanks.

oldbee
 
Old 01-17-2002, 03:36 PM   #2
parapente
Member
 
Registered: Sep 2001
Location: Greece
Distribution: Gentoo
Posts: 39

Rep: Reputation: 15
Lightbulb Idea!

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.
 
Old 01-17-2002, 03:45 PM   #3
oldbee
LQ Newbie
 
Registered: Jan 2002
Posts: 20

Original Poster
Rep: Reputation: 0
Good idea!

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)

I am using bash.

Thanks.

oldbee
 
Old 01-17-2002, 04:29 PM   #4
parapente
Member
 
Registered: Sep 2001
Location: Greece
Distribution: Gentoo
Posts: 39

Rep: Reputation: 15
Cool

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
 
Old 01-17-2002, 05:07 PM   #5
oldbee
LQ Newbie
 
Registered: Jan 2002
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks Parapente. It helps!
 
Old 01-18-2002, 01:47 AM   #6
dorward
Member
 
Registered: Sep 2001
Distribution: Gentoo
Posts: 760

Rep: Reputation: 31
An easier way would be to check to see if the $DISPLAY variable is set. If it is then you probably have an X server available, otherwise you don't.
 
Old 01-18-2002, 03:11 AM   #7
parapente
Member
 
Registered: Sep 2001
Location: Greece
Distribution: Gentoo
Posts: 39

Rep: Reputation: 15
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...
 
Old 01-18-2002, 09:08 AM   #8
oldbee
LQ Newbie
 
Registered: Jan 2002
Posts: 20

Original Poster
Rep: Reputation: 0
I agree with parapente. I thought about DISPLAY, but noticed it is just not "enough".
Thank you all.
 
Old 01-18-2002, 10:06 AM   #9
bluecadet
Member
 
Registered: Oct 2001
Distribution: MD81 RH71
Posts: 555

Rep: Reputation: 30
well yeah, having an X server running does not mean you are actually using it at that time...
 
Old 01-20-2002, 09:17 PM   #10
kervin
Member
 
Registered: Jan 2002
Location: Melbourne, FL.
Distribution: redhat
Posts: 168

Rep: Reputation: 31
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.

just my $0.02
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to refresh shell environment and remain sane primorec Linux - General 8 08-09-2005 11:09 PM
Shell bypass to graphical environment Stephanie Linux - General 6 09-05-2004 12:56 PM
What is a good GUI C++ environment for Debian? KDevelop? jdruin Debian 1 07-26-2004 08:14 PM
Sub shell environment variables tnine9 Programming 1 06-08-2004 01:27 PM
set environment variables using GUI? bax Linux - Software 2 04-10-2004 02:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration