Results 1 to 5 of 5

Thread: Test if shell script is called from a GUI session?

  1. #1
    Join Date
    Mar 2010
    Location
    Ohio
    Beans
    465
    Distro
    Kubuntu

    Test if shell script is called from a GUI session?

    Is there a way to test if a shell script is being called from a GUI session?

  2. #2
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Test if shell script is called from a GUI session?

    Quote Originally Posted by Ranko Kohime View Post
    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?

  3. #3
    Join Date
    Feb 2009
    Beans
    530
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Test if shell script is called from a GUI session?

    Quote Originally Posted by nvteighen View Post
    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.

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

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Test if shell script is called from a GUI session?

    Quote Originally Posted by jwbrase View Post
    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.

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

    Code:
    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
    Last edited by sisco311; February 7th, 2013 at 09:01 AM.

  5. #5
    Join Date
    Mar 2010
    Location
    Ohio
    Beans
    465
    Distro
    Kubuntu

    Re: Test if shell script is called from a GUI session?

    Quote Originally Posted by nvteighen View Post
    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.

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •