LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 02-09-2006, 08:34 AM   #1
bkeeper
LQ Newbie
 
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25

Rep: Reputation: 16
Cool 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
Code:
if [-a something]
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.
 
Old 02-09-2006, 09:26 AM   #2
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 32
test $XAUTHORITY || echo "not run from X" && exit
 
Old 02-09-2006, 11:33 AM   #3
Dtsazza
Member
 
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Rep: Reputation: 30
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.
 
Old 02-09-2006, 01:14 PM   #4
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
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.
 
Old 02-13-2006, 03:18 AM   #5
bkeeper
LQ Newbie
 
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25

Original Poster
Rep: Reputation: 16
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
 
Old 02-13-2006, 05:35 AM   #6
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 32
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...
 
Old 02-13-2006, 06:36 AM   #7
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: Ubuntu, FreeBSD, NetBSD
Posts: 1,449

Rep: Reputation: 47
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).
 
Old 02-13-2006, 08:52 AM   #8
bkeeper
LQ Newbie
 
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25

Original Poster
Rep: Reputation: 16
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.
 
Old 02-13-2006, 10:59 AM   #9
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: Ubuntu, FreeBSD, NetBSD
Posts: 1,449

Rep: Reputation: 47
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.
 
Old 02-21-2006, 06:31 AM   #10
bkeeper
LQ Newbie
 
Registered: Dec 2005
Location: Muenchen, Germany
Posts: 25

Original Poster
Rep: Reputation: 16
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Root user check in bash script doesn't work tawalker Programming 6 12-18-2011 03:06 AM
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM
logging a shell/bash session? JustinHoMi Linux - Software 6 04-09-2005 11:34 PM
Bash New Session bendeco13 Linux - General 2 10-30-2004 04:33 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM


All times are GMT -5. The time now is 10:01 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration