LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to have bash script check whether it's a x session? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-have-bash-script-check-whether-its-a-x-session-413432/)

bkeeper 02-09-2006 08:34 AM

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 :)

Nathanael 02-09-2006 09:26 AM

test $XAUTHORITY || echo "not run from X" && exit

Dtsazza 02-09-2006 11:33 AM

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

Hobbletoe 02-09-2006 01:14 PM

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.

bkeeper 02-13-2006 03:18 AM

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 :)

Nathanael 02-13-2006 05:35 AM

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...

frob23 02-13-2006 06:36 AM

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).

bkeeper 02-13-2006 08:52 AM

Wow! :eek:

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 :)

frob23 02-13-2006 10:59 AM

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. :)

bkeeper 02-21-2006 06:31 AM

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:39 AM.