SEARCH

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

You are not logged in.

#1 2015-09-23 04:29:27

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

[SOLVED] Any way to get return value from x-terminal-emulator window?

I've googled this and think it might be impossible, but just in case anyone here has an idea:

There's this script (dropbox pipemenu) from which I'd like to launch a sub-process in a terminal window. Like:

x-terminal-emulator -e sudo apt-get install package
# or
x-terminal-emulator -e bash -c 'some commands'

The thing is, the script wants to know if the commands succeeded or not but, after the window closes, x-terminal-emulator always seems to return 0.

Any thoughts?

Last edited by johnraff (2015-09-26 07:46:23)


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

Offline

Help fund CrunchBang, donate to the project!

#2 2015-09-23 08:28:03

iMBeCil
WAAAT?
From: Edrychwch o'ch cwmpas
Registered: 2012-03-22
Posts: 1,026
Website

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

johnraff wrote:

I've googled this and think it might be impossible, but just in case anyone here has an idea:

There's this script (dropbox pipemenu) from which I'd like to launch a sub-process in a terminal window. Like:

x-terminal-emulator -e sudo apt-get install package
# or
x-terminal-emulator -e bash -c 'some commands'

The thing is, the script wants to know if the commands succeeded or not but, after the window closes, x-terminal-emulator always seems to return 0.

Any thoughts?

A quick and dirty way to do it would be:

x-terminal-emulator -e bash -c 'some commands || echo $? > /tmp/somefile'

and checking '/tmp/somefile' afterwards in the script.

I didn't check it, and don't know if the syntax is correct, but I think you get the general idea.


Postpone all your duties; if you die, you won't have to do them ..
--> The very new BL forum! <--

Offline

#3 2015-09-23 09:22:20

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

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

After the child shell finishes, could the parent script check if the package is now present?


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

Offline

#4 2015-09-23 19:02:14

brontosaurusrex
#! Red Menace
Registered: 2012-06-15
Posts: 1,643

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

You have obviously tested the export thingy? (If it applies here anyway, I probably don't understand the question exactly)
http://stackoverflow.com/questions/7411 … rogramming

Last edited by brontosaurusrex (2015-09-23 19:03:33)

Offline

#5 2015-09-23 19:38:26

twoion
Moderator
Registered: 2012-05-11
Posts: 1,648

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

brontosaurusrex wrote:

You have obviously tested the export thingy? (If it applies here anyway, I probably don't understand the question exactly)
http://stackoverflow.com/questions/7411 … rogramming

This, together with imbecile's suggestion yields:

#!/bin/bash
export CATCH=/dev/shm/catch # POSIX shared memory is very useful for things like this…
x-terminal-emulator -e bash -c 'sudo apt-get install package; echo $? > $CATCH'
if [[ $(<$CATCH) -ne 0 ]]; then
  # error handling
fi

Tannhäuser ~ {www,pkg,ddl}.bunsenlabs.org/{gitlog,repoidx}

Offline

#6 2015-09-23 22:14:19

iMBeCil
WAAAT?
From: Edrychwch o'ch cwmpas
Registered: 2012-03-22
Posts: 1,026
Website

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

^Nice thing this '/dev/shm/' aka shared memory. Never heard of this before. Excellent info twoion, thanks a lot! smile


Postpone all your duties; if you die, you won't have to do them ..
--> The very new BL forum! <--

Offline

#7 2015-09-25 08:27:05

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

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

Thanks everybody - it looks as if an external temporary file is the only way out of this.
A lot of googling was triggered from these answers...

About /dev/shm, interesting to find that on Wheezy it's a symlink to /run/shm but on Jessie it's a self-standing directory. Web opinion seems divided 50/50 on whether it's Politically Correct to put random stuff in there, and/or whether memory caching makes the difference between /dev/shm and /tmp irrelevant in disk-write terms or not.

Exporting doesn't work for arrays. sad
But  it does work for functions. smile

Finally I might go with something like this (I might want to send some data in an array, and there is a workaround for exporting, since we're creating a temp file anyway):

john@bunsen:~$ ourfile=$(mktemp)
john@bunsen:~$ declare -A array=([one]="first element" [two]="second element")
john@bunsen:~$ declare -p array >"$ourfile"
john@bunsen:~$ export ourfile
john@bunsen:~$ stuff(){
. $ourfile
echo "${array[one]}"
echo "press a key"
read -srn1
[[ $REPLY ]] && return 1
return 0
}
john@bunsen:~$ export -f stuff
john@bunsen:~$ x-terminal-emulator -e bash -c 'stuff;echo $? > $ourfile'

** (x-terminal-emulator:18393): WARNING **: Binding '<Shift><Control>a' failed!
Unable to bind hide_window key, another instance/window has it.
# stuff from terminator - seems harmless

## another terminal opens with this:
first element
press a key
## press anything except Enter to get:

## back in the first terminal
john@bunsen:~$ cat $ourfile
1
## enter returns 0 instead

I used the same temp file to send in the array and get out the return value.
For giving the new terminal a lot of commands an exported function looks more reliable that putting the commands in a string variable.

Last edited by johnraff (2015-09-26 02:25:04)


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

Offline

#8 2015-09-25 08:54:56

brontosaurusrex
#! Red Menace
Registered: 2012-06-15
Posts: 1,643

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

Looks good, now if you want a more "posix" approach to mktemp, perhaps worth reading
http://mywiki.wooledge.org/BashFAQ/062

Offline

#9 2015-09-26 07:45:55

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

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

^Yes I read that page and figured since we have mktemp available in Debian, that was good enough. Re-reading it now I can see that making a user-owned temporary directory in /tmp to hold the tempfile could be a bit more secure, and not much harder to do.

damo wrote:

After the child shell finishes, could the parent script check if the package is now present?

A nice practical answer. Maybe such a check would be worth doing anyway.

I'm going to mark this solved, but please feel free to add any other comments...


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

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