LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums 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 06-28-2010, 06:12 PM   #16
GazL
LQ Veteran
 
Registered: May 2008
Distribution: CRUX
Posts: 7,369
Blog Entries: 1

Rep: Reputation: 5586Reputation: 5586Reputation: 5586Reputation: 5586Reputation: 5586Reputation: 5586Reputation: 5586Reputation: 5586Reputation: 5586Reputation: 5586Reputation: 5586

DELETED:
Just noticed this was a resurrected. I hate it when people do that!

Last edited by GazL; 06-28-2010 at 06:21 PM.
 
Old 06-29-2010, 10:26 AM   #17
oshazard
LQ Newbie
 
Registered: Jun 2010
Posts: 2

Rep: Reputation: 0
@GazL

You might hate that but I hate when I come to a thread via Google search results and find the resolved answer doesn't quite do it.
 
Old 06-29-2010, 01:19 PM   #18
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 614Reputation: 614Reputation: 614Reputation: 614Reputation: 614Reputation: 614
oshahazard, your answer is wrong because the '-s' test checks for the existence and non-zero-byte status of a file. DISPLAY is an environmental variable which is set when X is started. Checking if DISPLAY is a non-null string (-n) is the proper check.
 
0 members found this post helpful.
Old 09-09-2011, 01:18 PM   #19
jpolcari
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Rep: Reputation: Disabled
Surprised nobody got this yet.
Here's how I do it:

/usr/bin/xterm -e exit > /dev/null 2>&1
[ "$?" != "0" ] && exit
 
Old 09-09-2011, 01:21 PM   #20
jpolcari
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Rep: Reputation: Disabled
I should explain - your DISPLAY variable may set, but pointing nowhere.
If you login using putty for example, your DISPLAY gets set to something like localhost:10.0
but if you're not running a server where you came from, the DISPLAY is pointing to a non-existent server so
that's why I test it anyway.
 
Old 09-10-2011, 09:21 AM   #21
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by jpolcari View Post
Surprised nobody got this yet.
Here's how I do it:

/usr/bin/xterm -e exit > /dev/null 2>&1
[ "$?" != "0" ] && exit
Sergei Steshenko did suggest that back in article #10. He just didn't specify a particular program. Really, I don't see how running some other program with the possibility of it's failure is any different from running the program of principal interest, and having it possibly fail.

The only real test is to check whether there is a $DISPLAY variable set to a sane value, and then try to make an X connection to the indicated server. The X client application does that by default. One could probably contrive a special purpose application that does only that, and sets return values indicating success or its particular failure mode.
The problem with using the process table to check for a running X server is of course that the X server may not be running on the same host that the application will run on, and there is the possibility the any local X server is not named 'X', and that the X server may not be accepting connections from application(s) on the local host. There is also the possibility that the running X server does not support some other required functionality, such as video modes, fonts, etc. None of these conditions are easy to detect without simply trying to make the connection and use the server.

--- rod.

Last edited by theNbomr; 09-10-2011 at 09:23 AM.
 
Old 09-10-2011, 05:22 PM   #22
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
Really, I don't see how running some other program with the possibility of it's failure is any different from running the program of principal interest, and having it possibly fail.
I think that's the best possible solution, if it follows a check for the readability of the file. There are too many possible problems with trying to intuit whether or not the X program will work; it's better just to test it with a set of options that are known to be good. Elaborating on MTK358's solution:
Code:
show_image()
{
    [ -r "$1" ] || return 1
    file "$1" | grep -q 'image data' || return 1 #(or whatever format checks are needed)
    display options "$1" || cacaview other-options "$1"
}

show_image my.bmp
Kevin Barry

Last edited by ta0kira; 09-10-2011 at 05:24 PM.
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Running a BASH Script within PERL pm atrac Programming 4 05-10-2008 12:09 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
why this script is running in bash but not in perl ratul_11 Linux - General 2 08-17-2007 09:31 AM
perl/bash script to monitor all processes running in my machine pudhiyavan Linux - Security 4 07-19-2005 02:09 PM

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

All times are GMT -5. The time now is 06:30 PM.

Contact Us - Advertising Info - Rules - Privacy - Donations - Contributing Member - LQ Sitemap - "Weather apps tell you it'll rain. Wyndo tells you when to go."
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