PDA

View Full Version : Test if shell script is called from a GUI session?



Ranko Kohime
February 6th, 2013, 06:35 PM
Is there a way to test if a shell script is being called from a GUI session?

nvteighen
February 6th, 2013, 08:19 PM
Is there a way to test if a shell script is being called from a GUI session?

What do you mean by "being called from a GUI session"? Whether the script has been called from a terminal emulator (as opposed to the console)? Or whether a script has been called from a GUI program in general (e.g. via system(), popen(), etc.).?

In any case, I don't really get why the importance of this. If you're the shell script, you shouldn't really care where you are run from, but just do what you are supposed to do. Care to elaborate?

jwbrase
February 7th, 2013, 03:52 AM
In any case, I don't really get why the importance of this. If you're the shell script, you shouldn't really care where you are run from, but just do what you are supposed to do.

Sometimes what you're supposed to do may depend on where you're run from. For example, you may want to use zenity dialogs as your interface if you're running under X, but fall back to a text interface if not.


@OP:
You can test to see if your program is running under X by testing to see if the "display" environment variable is defined.


if[ -n $DISPLAY ]
then
#Stuff to do if running under X
else
#stuff to do if not running under X
fi

This won't work if your OS uses something other than X for its GUI, but that's not an issue *yet* on most Unix-like systems (but MacOS/iOS and Android are prominent exceptions, and Wayland may eventually replace X on other Unix-likes).

sisco311
February 7th, 2013, 08:51 AM
Sometimes what you're supposed to do may depend on where you're run from. For example, you may want to use zenity dialogs as your interface if you're running under X, but fall back to a text interface if not.


@OP:
You can test to see if your program is running under X by testing to see if the "display" environment variable is defined.


if[ -n $DISPLAY ]
then
#Stuff to do if running under X
else
#stuff to do if not running under X
fi

This won't work if your OS uses something other than X for its GUI, but that's not an issue *yet* on most Unix-like systems (but MacOS/iOS and Android are prominent exceptions, and Wayland may eventually replace X on other Unix-likes).

Then, I'd use the good old `trial and error' method:



if zenity "$z_options"
then
printf '%s\n' "zenity is it" >&2
elif dialog "$d_options"
then
printf '%s\n' "hurray, we are using dialog" >&2
else
printf '%s\n' "err" >&2
fi

Ranko Kohime
February 7th, 2013, 04:35 PM
What do you mean by "being called from a GUI session"? Whether the script has been called from a terminal emulator (as opposed to the console)? Or whether a script has been called from a GUI program in general (e.g. via system(), popen(), etc.).?

In any case, I don't really get why the importance of this. If you're the shell script, you shouldn't really care where you are run from, but just do what you are supposed to do. Care to elaborate?
Oh, right. Forgot the purpose. :oops:

My script is long running, and its going to be putting out status messages. If I'm in a CLI session, I want to echo those directly to the screen. If I'm in a terminal emulator, though, (and it probably won't be called from another program than an emulator) I want to route it through notify-send.