SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2015-05-17 01:41:13

damo
#! gimpbanger
From: N51.5 W002.8 (mostly)
Registered: 2011-11-24
Posts: 5,434

[SOLVED] Script commands to find x-terminal-emulator being used?

I'm trying to run a command with x-terminal-emulator from a script, but different terms have different requirements. For example, BL default is xfce4-terminal, which is run via a wrapper script called with x-terminal-emulator, so it uses

x-terminal-emulator -geometry (width-chars)x(height-chars) -e <command>

but terminator for example doesn't seem to honour the X standards so the same command needs

x-terminal-emulator --geometry (width-px)x(height-px) --command <command>

Is there a simple way to determine which terminal has been set with update-alternatives, so case statements could be used to choose the relevant syntax? Extensive googling hasn't come up with the goods sad


EDIT This clumsy one-liner looks like it would do the job, but a more elegant way would be nice wink

update-alternatives --display x-terminal-emulator | grep currently | awk -F"/" '{print $NF}'


A SOLUTION (if anyone is interested!)
This snippet opens a small terminal in the centre of the screen, for getting the password for $CMD (which needs sudo)

    SCREENDIMS=$(xdpyinfo | grep dimensions | awk '{print $2}')
    LEFT=$(echo $SCREENDIMS | awk -F"x" '{print $1}')
    LEFT=$(($LEFT/2-246))
    TOP=$(echo $SCREENDIMS | awk -F"x" '{print $2}')
    TOP=$(($TOP/2-36))
    # get x-terminal-emulator in use
    TERMINAL=$(update-alternatives --display x-terminal-emulator | grep currently | awk -F"/" '{print $NF}')
    case $TERMINAL in
        gnome-terminal.wrapper  ) gnome-terminal --geometry 70x5+$LEFT+$TOP -e "$CMD";;
        terminator              ) terminator --geometry 492x72+$LEFT+$TOP -T "$TXT" --command "$CMD";;
        *                       ) x-terminal-emulator -geometry 70x5+$LEFT+$TOP -T "$TXT" -e "$CMD";;
    esac

Last edited by damo (2015-05-17 03:57:36)


BunsenLabs Group on deviantArt
damo's gallery on deviantArt
Openbox themes
Forum Moderator smile

Offline

Be excellent to each other!

#2 2015-05-17 04:19:59

porkpiehat
#! Die Hard
Registered: 2012-10-02
Posts: 1,007

Re: [SOLVED] Script commands to find x-terminal-emulator being used?

damo wrote:

EDIT This clumsy one-liner looks like it would do the job, but a more elegant way would be nice wink

update-alternatives --display x-terminal-emulator | grep currently | awk -F"/" '{print $NF}'

Try this:

TERMINAL=$(update-alternatives --query x-terminal-emulator | awk -F/ '/^Value/ {print $NF}')

awk can grep, so this uses one less pipe -- you can also grep with awk in your other code:

SCREENDIMS=$(xdpyinfo | awk '/dimensions/ {print $2}')

I am a little confused as to why you are using a terminal to do this though -- wouldn't it be simpler to use a dialog?

Last edited by porkpiehat (2015-05-17 04:43:08)

Offline

#3 2015-05-17 04:53:13

damo
#! gimpbanger
From: N51.5 W002.8 (mostly)
Registered: 2011-11-24
Posts: 5,434

Re: [SOLVED] Script commands to find x-terminal-emulator being used?

^ TY for the grep/awk advice, cos my brain hurts with this stuff!

The problem with a dialog is that if you input the wrong password it just closes - at least that is what I have found with both gksudo and zenity. I haven't found a way of getting the return value with gksudo, and checking password hashes for zenity looks like a minefield.

With sudo in a terminal you get a password check and several attempts if it is wrong  hmm

There would be much more code to write for a dialog AFAICS, to include error checks etc.


BunsenLabs Group on deviantArt
damo's gallery on deviantArt
Openbox themes
Forum Moderator smile

Offline

#4 2015-05-17 05:44:50

cpoakes
#! CrunchBanger
From: Tucson, Arizona
Registered: 2012-05-19
Posts: 202

Re: [SOLVED] Script commands to find x-terminal-emulator being used?

Debian Policy 11.8.3 requires only two options of x-terminal-emulator: -T (title), and -e (execute remaining arguments). Terminator version 0.97-3 was modified to make -e work correctly; substituting --command is NOT required when using the jessie version (but it is required for wheezy). The geometry spec is not specified as part of Debian policy, though many wrapper scripts attempt full xterm compatibility.  Though -g does not work with terminator, -g does work for many others when -geometry or --geometry are not supported.

Identifying the actual terminal is much simpler; no awk or grep required:

REALTERMINAL=`readlink -f /usr/bin/x-terminal-emulator`

programming and administering unix since 1976 (BSD, System III, Xenix, System V, Linux)

Offline

#5 2015-05-17 12:46:38

damo
#! gimpbanger
From: N51.5 W002.8 (mostly)
Registered: 2011-11-24
Posts: 5,434

Re: [SOLVED] Script commands to find x-terminal-emulator being used?

cpoakes wrote:

Debian Policy 11.8.3 requires only two options of x-terminal-emulator: -T (title), and -e (execute remaining arguments). Terminator version 0.97-3 was modified to make -e work correctly; substituting --command is NOT required when using the jessie version (but it is required for wheezy). The geometry spec is not specified as part of Debian policy, though many wrapper scripts attempt full xterm compatibility.  Though -g does not work with terminator, -g does work for many others when -geometry or --geometry are not supported.

Identifying the actual terminal is much simpler; no awk or grep required:

REALTERMINAL=`readlink -f /usr/bin/x-terminal-emulator`

TY for the "REALTERMINAL" tip

Whatever the official policies are, there are issues with variable expansion, because I get this when running terminator from a script:
(just to test sudo BTW).
No problem with...

CMD="sudo touch tempfile"
terminator --command "$CMD"

But I get "Unable to find a shell" with...

CMD="sudo touch tempfile"
terminator -e "$CMD"

This works though...

CMD="sudo touch tempfile"
terminator -e $CMD

This works...

CMD="sudo touch tempfile"
xfce4-terminal -e "$CMD"

BunsenLabs Group on deviantArt
damo's gallery on deviantArt
Openbox themes
Forum Moderator smile

Offline

#6 2015-05-17 13:13:56

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: [SOLVED] Script commands to find x-terminal-emulator being used?

damo wrote:

But I get "Unable to find a shell" with...

CMD="sudo touch tempfile"
terminator -e "$CMD"

This works though...

CMD="sudo touch tempfile"
terminator -e $CMD

I think the second version is the one that should work. urxvt will also reject a command+args line if it's wrapped in quotes. xfce4-terminal is trying to be helpful or something. In fact, the standards-compliant call is xfce4-terminal.wrapper which takes the unquoted line OK.

If I've got this right, the -e syntax means

command arg1 arg2...

ie the command and arguments are separate words.

"command arg1 arg2"

would have the terminal trying to execute that as one single command. Although commands can't contain spaces of course...


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

Offline

#7 2015-05-17 13:20:50

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: [SOLVED] Script commands to find x-terminal-emulator being used?

damo wrote:

The problem with a dialog is that if you input the wrong password it just closes - at least that is what I have found with both gksudo and zenity. I haven't found a way of getting the return value with gksudo, ...

Doesn't gksu just return 1? That's what I got:

john@raffles4:~$ gksu leafpad
# enter wrong password
# couple of seconds delay (why?), then...
(gksu:6450): GLib-CRITICAL **: g_str_has_prefix: assertion `str != NULL' failed
john@raffles4:~$ echo $?
1

John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

Offline

#8 2015-05-17 13:58:47

damo
#! gimpbanger
From: N51.5 W002.8 (mostly)
Registered: 2011-11-24
Posts: 5,434

Re: [SOLVED] Script commands to find x-terminal-emulator being used?

^ Course it does! Silly damo was trying to get the value from a var like

RETURN=$(gksudo <command>)

I forgot about "$?"  :8

PS I think the delay is because of the pauses and repeat password requests behind the gui...

[damo@cb-desktop ~]$ sudo su
[sudo] password for damo: 
Sorry, try again.
[sudo] password for damo: 
Sorry, try again.
[sudo] password for damo: 
sudo: 3 incorrect password attempts
[damo@cb-desktop ~]$

BunsenLabs Group on deviantArt
damo's gallery on deviantArt
Openbox themes
Forum Moderator smile

Offline

#9 2015-05-17 14:16:34

Head_on_a_Stick
CatMod
From: A world of pure imagination
Registered: 2014-01-21
Posts: 4,797

Re: [SOLVED] Script commands to find x-terminal-emulator being used?

Slightly off-topic (sorry!), but if you use zsh with grml's configuration file [1], the error value is returned automatically as a prefix on the subsequent command lines:

empty@BunsenLabs ~ % gksu geany
3 empty@BunsenLabs ~ % echo $?                                                                    :(
3

cool

[1] http://crunchbang.org/forums/viewtopic.php?id=39330

Offline

#10 2015-05-17 15:09:02

damo
#! gimpbanger
From: N51.5 W002.8 (mostly)
Registered: 2011-11-24
Posts: 5,434

Re: [SOLVED] Script commands to find x-terminal-emulator being used?

@HoaS

I find working with ONE shell hard enough, never mind trying to get to grips with a new one!  :8  lol

(No need to apologize for providing knowledge though...)


BunsenLabs Group on deviantArt
damo's gallery on deviantArt
Openbox themes
Forum Moderator smile

Offline

#11 2015-05-17 15:46:12

damo
#! gimpbanger
From: N51.5 W002.8 (mostly)
Registered: 2011-11-24
Posts: 5,434

Re: [SOLVED] Script commands to find x-terminal-emulator being used?

@johnraff et al

TY smile

Simples...

    if ! gksudo -m "$TXT" "$CMD"; then
        MSG="Password input failed\n\nlightdm-gtk-greeter.conf was not restored"
        zenity --warning --text "$MSG"
    fi

(...although I have learned something about x-terminal-emulator on the way  wink )

Last edited by damo (2015-05-17 15:46:53)


BunsenLabs Group on deviantArt
damo's gallery on deviantArt
Openbox themes
Forum Moderator smile

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo