Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
02-09-2006, 08:34 AM
|
#1
|
LQ Newbie
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25
Rep:
|
how to have bash script check whether it's a x session?
Hi folks
This is going to be an easy one for somebody who knows...  how can I check in a bash script whether it's a x session or not?
I'm doing a xmodmap in my ~/.profile, however, when it's a text only login, there's an error message which I'd like to avoid... a bit aesthetics here... *g* I guess a
will do it?
And, where else but here could I have found out this?
Thank you already in advance!
Cheers,
bkeeper 
Last edited by bkeeper; 02-09-2006 at 08:42 AM.
|
|
|
02-09-2006, 09:26 AM
|
#2
|
Member
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940
Rep:
|
test $XAUTHORITY || echo "not run from X" && exit
|
|
|
02-09-2006, 11:33 AM
|
#3
|
Member
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282
Rep:
|
Hmm, whilst I don't have anything constructive to add to this, I don't have the XAUTHORITY envvar set, and running your commands (without the exit on the end) always tell me I'm not in X, even when I am.
At a guess, since everything's a file in Linux, presumably starting X will create a screen file somewhere (localhost:0), and you could check for that?
Edit: OK, I just searched for .*:0.* and got ~/.DCOPserver_$HOSTNAME_:0, but that's KDE-centric. Not so good, though it might form part of a test if it's quicker than the definitive method - assuming that the file is destroyed with X, which I haven't checked.
Another approach is processes - if X is running, you can see it with ps aux. However, that brings its own problems (why couldn't it have a longer and more unique name?  ) - my output is
Code:
root 5133 0.4 7.8 91424 71568 ? R Feb01 49:25 /usr/X11R6/bin/X -nolisten tcp :0 vt7 -auth /var/run/xauth/A:0-4ow2QF
so there's a lot of gunk to strip out. Still, with judicious use of awk/cut and basename you could check the command of every process, and there you can guarantee that it'll be called X.
HTH,
Andrzej
Last edited by Dtsazza; 02-09-2006 at 11:39 AM.
|
|
|
02-09-2006, 01:14 PM
|
#4
|
Member
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150
Rep:
|
If you are getting an error that you don't want to see, can't you just redirect that error message to /dev/null?
Code:
xmodmap whatever 2> /dev/null
The command will still run, but any errors that occur will be sent to the digital bit bucket. You shouldn't have to check to see if it is an xsession or not.
The problem with that, of course, is that if something goes wrong, you get no output about it. It should clear up your problem, but might make it harder to track down a problem in the future.
|
|
|
02-13-2006, 03:18 AM
|
#5
|
LQ Newbie
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25
Original Poster
Rep:
|
Hi folks,
thanks for the replies. Indeed, $XAUTHORITY is not set on my system. I guess Andrzej way of using ps, grepping for X, is the one to go to do it properly. 
[ ...However, I'll settle with Hobbletoe's version for the moment...  ]
Thanks again!
Cheers,
bkeeper 
|
|
|
02-13-2006, 05:35 AM
|
#6
|
Member
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940
Rep:
|
checking ps of X will not help anyway, because somebody could ssh in to run the sc ript, or just use tty1 while X is running...
|
|
|
02-13-2006, 06:36 AM
|
#7
|
Senior Member
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450
Rep:
|
Have you tried testing for $DISPLAY?
That value should always be set for an X session and never for a session not under X -- unless you manually set it in your login scripts (I have done this when I was routinely logging in and starting X programs I wanted to see on my local display).
|
|
|
02-13-2006, 08:52 AM
|
#8
|
LQ Newbie
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25
Original Poster
Rep:
|
Wow!
So it's as simple as
Code:
if [ $DISPLAY ]; then
xmodmap ~/.xmodmap
fi
That's cool. And it even works... *g* Thank you!
Cheers,
bkeeper 
Last edited by bkeeper; 02-13-2006 at 08:54 AM.
|
|
|
02-13-2006, 10:59 AM
|
#9
|
Senior Member
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450
Rep:
|
Yep... that should work without a problem.
Just one question... why are you doing this in ~/.profile and not ~/.xinitrc and ~/.xsession ?
Those are the two files which "traditionally" call programs which modify X.
Just put a line like:
Code:
xmodmap ~/.xmodmap &
somewhere above the last line in the file(s) -- and make sure you have the "&" because it is important -- and it should run just fine.
Of course, you're at liberty to do this however you want. I just didn't realize what you were doing at first; so, I answered the question without thinking about the actual problem you were trying to solve. 
Last edited by frob23; 02-13-2006 at 11:02 AM.
|
|
|
02-21-2006, 06:31 AM
|
#10
|
LQ Newbie
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25
Original Poster
Rep:
|
Hi frob,
yes, you're right. However, for some weird reason, it's not executed if I put it in one of the ~/.x files. Actually, it seems these files are not executed on my system at all. But as it's a business machine which I don't administrate, at the end of the day, I don't really care as long as it works - which it does now, thanks again! 
Cheers,
bkeeper 
|
|
|
All times are GMT -5. The time now is 08:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|